orm useIndex change

This commit is contained in:
yuri
2019-10-08 13:43:31 +03:00
parent b21b69eed5
commit 6c8a0bbb66
3 changed files with 39 additions and 8 deletions

View File

@@ -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);

View File

@@ -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') {

View File

@@ -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()) {