diff --git a/application/Espo/ORM/DB/Query/Base.php b/application/Espo/ORM/DB/Query/Base.php index 6960c8d9de..4c3ecbd8db 100644 --- a/application/Espo/ORM/DB/Query/Base.php +++ b/application/Espo/ORM/DB/Query/Base.php @@ -57,7 +57,7 @@ abstract class Base 'customHaving', 'skipTextColumns', 'maxTextColumnsLength', - 'useIndexList', + 'useIndex', ]; protected static $sqlOperators = [ @@ -386,9 +386,14 @@ abstract class Base } $indexKeyList = null; - if (!empty($params['useIndexList']) && $this->metadata) { + $indexList = $params['useIndex'] ?? null; + + if (!empty($indexList) && $this->metadata) { $indexKeyList = []; - foreach ($params['useIndexList'] as $indexName) { + if (is_string($indexList)) { + $indexList = [$indexList]; + } + foreach ($indexList as $indexName) { $indexKey = $this->metadata->get($entityType, ['indexes', $indexName, 'key']); if ($indexKey) { $indexKeyList[] = $indexKey; @@ -1819,8 +1824,34 @@ abstract class Base $midAlias = $alias . 'Middle'; + $indexKeyList = null; + $indexList = $params['useIndex'] ?? null; + + if ($indexList && $this->metadata) { + $indexKeyList = []; + if (is_string($indexList)) { + $indexList = [$indexList]; + } + foreach ($indexList as $indexName) { + $indexKey = $this->metadata->get($entity->getEntityType(), ['relations', $relationName, 'indexes', $indexName, 'key']); + if ($indexKey) { + $indexKeyList[] = $indexKey; + } + } + } + + $indexPart = ''; + + if ($indexKeyList && count($indexKeyList)) { + $sanitizedIndexList = []; + foreach ($indexKeyList as $indexKey) { + $sanitizedIndexList[] = '`' . $this->sanitizeIndexName($indexKey) . '`'; + } + $indexPart = " USE INDEX (".implode(', ', $sanitizedIndexList).")"; + } + $sql = - "{$prefix}JOIN `{$relTable}` AS `{$midAlias}` ON {$this->toDb($entity->getEntityType())}." . $this->toDb($key) . " = {$midAlias}." . $this->toDb($nearKey) + "{$prefix}JOIN `{$relTable}` AS `{$midAlias}`{$indexPart} ON {$this->toDb($entity->getEntityType())}." . $this->toDb($key) . " = {$midAlias}." . $this->toDb($nearKey) . " AND " . "{$midAlias}.deleted = " . $this->pdo->quote(0); diff --git a/application/Espo/SelectManagers/Email.php b/application/Espo/SelectManagers/Email.php index 9454e6f926..3e263937e8 100644 --- a/application/Espo/SelectManagers/Email.php +++ b/application/Espo/SelectManagers/Email.php @@ -71,12 +71,12 @@ class Email extends \Espo\Core\SelectManagers\Base $skipIndex = true; } if (!$skipIndex) { - $result['useIndexList'] = ['dateSent']; + $result['useIndex'] = 'dateSent'; } } if ($folderId === 'drafts') { - $result['useIndexList'] = ['createdById']; + $result['useIndex'] = 'createdById'; } if ($folderId !== 'drafts') { diff --git a/application/Espo/Services/Stream.php b/application/Espo/Services/Stream.php index f58a9d4d99..8fa50a77a5 100644 --- a/application/Espo/Services/Stream.php +++ b/application/Espo/Services/Stream.php @@ -386,7 +386,7 @@ class Stream extends \Espo\Core\Services\Base 'orderBy' => 'number', 'order' => 'DESC', 'limit' => $sqLimit, - 'useIndexList' => ['createdByNumber'], + 'useIndex' => 'createdByNumber', ]; if ($user->isPortal()) { @@ -484,7 +484,7 @@ class Stream extends \Espo\Core\Services\Base 'orderBy' => 'number', 'order' => 'DESC', 'limit' => $sqLimit, - 'useIndexList' => ['createdByNumber'], + 'useIndex' => 'createdByNumber', ]; if ($user->isPortal()) {