From 0d4a6d0b679aa76d7198d800e795dada164c3048 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Wed, 31 Mar 2021 17:26:40 -0500 Subject: [PATCH 1/6] MC-41647: SKU Search very slow on Admin Panel --- .../Model/ResourceModel/Search/Collection.php | 84 +++++++++++++++---- 1 file changed, 67 insertions(+), 17 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php b/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php index d37f0f8a5153b..eab88a5d5ca95 100644 --- a/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php +++ b/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php @@ -17,6 +17,10 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection implements \Magento\Search\Model\SearchCollectionInterface { + private const INDEX_USAGE_ENFORCEMENTS = [ + 'catalog_product_entity_text' => 'CATALOG_PRODUCT_ENTITY_TEXT_ROW_ID_ATTRIBUTE_ID_STORE_ID' + ]; + /** * Attribute collection * @@ -197,6 +201,21 @@ protected function _hasAttributeOptionsAndSearchable($attribute) return false; } + /** + * Prepare table names for the index enforcements + * + * @return array + */ + private function prepareIndexEnforcements() : array + { + $result = []; + foreach (self::INDEX_USAGE_ENFORCEMENTS as $table => $index) { + $table = $this->getTable($table); + $result[$table] = $index; + } + return $result; + } + /** * Retrieve SQL for search entities * @@ -208,6 +227,7 @@ protected function _getSearchEntityIdsSql($query, $searchOnlyInCurrentStore = tr { $tables = []; $selects = []; + $preparedIndexEnforcements = $this->prepareIndexEnforcements(); $likeOptions = ['position' => 'any']; @@ -249,23 +269,53 @@ protected function _getSearchEntityIdsSql($query, $searchOnlyInCurrentStore = tr $ifValueId = $this->getConnection()->getIfNullSql('t2.value', 't1.value'); foreach ($tables as $table => $attributeIds) { - $selects[] = $this->getConnection()->select()->from( - ['t1' => $table], - $linkField - )->joinLeft( - ['t2' => $table], - $joinCondition, - [] - )->where( - 't1.attribute_id IN (?)', - $attributeIds, - \Zend_Db::INT_TYPE - )->where( - 't1.store_id = ?', - 0 - )->where( - $this->_resourceHelper->getCILike($ifValueId, $this->_searchQuery, $likeOptions) - ); + if (!empty($preparedIndexEnforcements[$table])) { + $condition1 = $this->_conn->quoteInto( + '`t1`.`attribute_id` IN (?)', + $attributeIds, + \Zend_Db::INT_TYPE + ); + $condition2 = '`t1`.`store_id` = 0'; + $condition3 = $this->_conn->quoteInto( + 'IFNULL(`t2`.`value`, `t1`.`value`) LIKE ?', + $this->_resourceHelper->addLikeEscape($this->_searchQuery, $likeOptions) + ); + + //force index statement not implemented in framework + // phpcs:ignore Magento2.SQL.RawQuery + $select = sprintf( + 'SELECT `t1`.`%s` FROM `%s` AS `t1` FORCE INDEX(%s) LEFT JOIN `%s` AS `t2` + ON %s WHERE %s AND %s AND %s', + $linkField, + $table, + $preparedIndexEnforcements[$table], + $table, + $joinCondition, + $condition1, + $condition2, + $condition3 + ); + } else { + $select = $this->getConnection()->select(); + $select->from( + ['t1' => $table], + $linkField + )->joinLeft( + ['t2' => $table], + $joinCondition, + [] + )->where( + 't1.attribute_id IN (?)', + $attributeIds, + \Zend_Db::INT_TYPE + )->where( + 't1.store_id = ?', + 0 + )->where( + $this->_resourceHelper->getCILike($ifValueId, $this->_searchQuery, $likeOptions) + ); + } + $selects[] = $select; } $sql = $this->_getSearchInOptionSql($query); From 4b705fc309900df25a17f98ea887452c1f6827ae Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Thu, 1 Apr 2021 09:24:25 -0500 Subject: [PATCH 2/6] MC-41647: SKU Search very slow on Admin Panel --- .../CatalogSearch/Model/ResourceModel/Search/Collection.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php b/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php index eab88a5d5ca95..c4a4b39d97dce 100644 --- a/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php +++ b/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php @@ -276,8 +276,9 @@ protected function _getSearchEntityIdsSql($query, $searchOnlyInCurrentStore = tr \Zend_Db::INT_TYPE ); $condition2 = '`t1`.`store_id` = 0'; + $quotedField = $this->_conn->quoteIdentifier($ifValueId); $condition3 = $this->_conn->quoteInto( - 'IFNULL(`t2`.`value`, `t1`.`value`) LIKE ?', + $quotedField . ' LIKE ?', $this->_resourceHelper->addLikeEscape($this->_searchQuery, $likeOptions) ); @@ -285,7 +286,7 @@ protected function _getSearchEntityIdsSql($query, $searchOnlyInCurrentStore = tr // phpcs:ignore Magento2.SQL.RawQuery $select = sprintf( 'SELECT `t1`.`%s` FROM `%s` AS `t1` FORCE INDEX(%s) LEFT JOIN `%s` AS `t2` - ON %s WHERE %s AND %s AND %s', + ON %s WHERE %s AND %s AND (%s)', $linkField, $table, $preparedIndexEnforcements[$table], From aa9485a17d515ee0ac6ecf74103f06ab57a6339f Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Mon, 5 Apr 2021 17:40:39 -0500 Subject: [PATCH 3/6] MC-41647: SKU Search very slow on Admin Panel --- .../Magento/Backend/Controller/Adminhtml/IndexTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/integration/testsuite/Magento/Backend/Controller/Adminhtml/IndexTest.php b/dev/tests/integration/testsuite/Magento/Backend/Controller/Adminhtml/IndexTest.php index d5a48b960811e..6ddcb2ddfe3fe 100644 --- a/dev/tests/integration/testsuite/Magento/Backend/Controller/Adminhtml/IndexTest.php +++ b/dev/tests/integration/testsuite/Magento/Backend/Controller/Adminhtml/IndexTest.php @@ -9,7 +9,7 @@ /** * @magentoAppArea adminhtml - * @magentoDbIsolation enabled + * @magentoDbIsolation disabled */ class IndexTest extends \Magento\TestFramework\TestCase\AbstractBackendController { From df9c4ef67b867461d5a75460b35d8379bed6b84d Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Mon, 5 Apr 2021 18:12:01 -0500 Subject: [PATCH 4/6] MC-41647: SKU Search very slow on Admin Panel --- .../Model/ResourceModel/Search/Collection.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php b/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php index c4a4b39d97dce..d37f816e42cdb 100644 --- a/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php +++ b/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php @@ -211,11 +211,25 @@ private function prepareIndexEnforcements() : array $result = []; foreach (self::INDEX_USAGE_ENFORCEMENTS as $table => $index) { $table = $this->getTable($table); - $result[$table] = $index; + if ($this->isIndexExists($table, $index)) { + $result[$table] = $index; + } } return $result; } + /** + * Check if index exists in the table + * + * @param string $table + * @param string $index + * @return bool + */ + private function isIndexExists(string $table, string $index) : bool + { + return array_key_exists($index, $this->_conn->getIndexList($table)); + } + /** * Retrieve SQL for search entities * From 94f2661af768a75bef6027c9cc3015886ab6e142 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 6 Apr 2021 10:17:02 -0500 Subject: [PATCH 5/6] MC-41647: SKU Search very slow on Admin Panel --- .../Model/ResourceModel/Search/Collection.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php b/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php index d37f816e42cdb..4cbe17646fbda 100644 --- a/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php +++ b/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php @@ -17,9 +17,10 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection implements \Magento\Search\Model\SearchCollectionInterface { - private const INDEX_USAGE_ENFORCEMENTS = [ - 'catalog_product_entity_text' => 'CATALOG_PRODUCT_ENTITY_TEXT_ROW_ID_ATTRIBUTE_ID_STORE_ID' - ]; + /** + * @var array + */ + private $indexUsageEnforcements; /** * Attribute collection @@ -65,6 +66,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection + * @param array $indexUsageEnforcements * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -88,7 +90,8 @@ public function __construct( \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Customer\Api\GroupManagementInterface $groupManagement, \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory, - \Magento\Framework\DB\Adapter\AdapterInterface $connection = null + \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, + array $indexUsageEnforcements = [] ) { $this->_attributeCollectionFactory = $attributeCollectionFactory; parent::__construct( @@ -113,6 +116,7 @@ public function __construct( $groupManagement, $connection ); + $this->indexUsageEnforcements = $indexUsageEnforcements; } /** @@ -209,7 +213,7 @@ protected function _hasAttributeOptionsAndSearchable($attribute) private function prepareIndexEnforcements() : array { $result = []; - foreach (self::INDEX_USAGE_ENFORCEMENTS as $table => $index) { + foreach ($this->indexUsageEnforcements as $table => $index) { $table = $this->getTable($table); if ($this->isIndexExists($table, $index)) { $result[$table] = $index; From 1bc28f497d688d4cfbfdf0f2792ec63d2e00d54b Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 6 Apr 2021 10:42:49 -0500 Subject: [PATCH 6/6] MC-41647: SKU Search very slow on Admin Panel --- .../CatalogSearch/Model/ResourceModel/Search/Collection.php | 4 +++- .../Magento/Backend/Controller/Adminhtml/IndexTest.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php b/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php index 4cbe17646fbda..7e9be408a3850 100644 --- a/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php +++ b/app/code/Magento/CatalogSearch/Model/ResourceModel/Search/Collection.php @@ -303,12 +303,14 @@ protected function _getSearchEntityIdsSql($query, $searchOnlyInCurrentStore = tr //force index statement not implemented in framework // phpcs:ignore Magento2.SQL.RawQuery $select = sprintf( - 'SELECT `t1`.`%s` FROM `%s` AS `t1` FORCE INDEX(%s) LEFT JOIN `%s` AS `t2` + 'SELECT `t1`.`%s` FROM `%s` AS `t1` FORCE INDEX(%s) + LEFT JOIN `%s` AS `t2` FORCE INDEX(%s) ON %s WHERE %s AND %s AND (%s)', $linkField, $table, $preparedIndexEnforcements[$table], $table, + $preparedIndexEnforcements[$table], $joinCondition, $condition1, $condition2, diff --git a/dev/tests/integration/testsuite/Magento/Backend/Controller/Adminhtml/IndexTest.php b/dev/tests/integration/testsuite/Magento/Backend/Controller/Adminhtml/IndexTest.php index 6ddcb2ddfe3fe..d5a48b960811e 100644 --- a/dev/tests/integration/testsuite/Magento/Backend/Controller/Adminhtml/IndexTest.php +++ b/dev/tests/integration/testsuite/Magento/Backend/Controller/Adminhtml/IndexTest.php @@ -9,7 +9,7 @@ /** * @magentoAppArea adminhtml - * @magentoDbIsolation disabled + * @magentoDbIsolation enabled */ class IndexTest extends \Magento\TestFramework\TestCase\AbstractBackendController {