Skip to content

Instantly share code, notes, and snippets.

@erikhansen
Last active April 29, 2021 08:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikhansen/a813f8cf9fe8e9836eea467bdd205659 to your computer and use it in GitHub Desktop.
Save erikhansen/a813f8cf9fe8e9836eea467bdd205659 to your computer and use it in GitHub Desktop.
Patch for `INSERT INTO search_tmp_` search issue: https://github.com/magento/magento2/issues/15545
{
"name": "magento/project-community-edition",
// …
// After installing https://github.com/cweagans/composer-patches, make this change to your composer.json file and then
// run `composer install` and `composer update --lock`
"extra": {
"magento-force": "override",
"composer-exit-on-patch-failure": true,
"patches": {
"magento/module-catalog-search": {
"Fix hanging `INSERT INTO search_tmp_` query - Patch created by referencing http://bit.ly/2OcN8a4 and http://bit.ly/2QN3p7d": "patches/composer/module-catalog-search-fix-insert-into-hanging.patch"
}
}
}
}
Index: Model/Search/FilterMapper/CustomAttributeFilter.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Model/Search/FilterMapper/CustomAttributeFilter.php (date 1574535757476)
+++ Model/Search/FilterMapper/CustomAttributeFilter.php (date 1574535757476)
@@ -98,8 +98,21 @@
$attributes[] = $attributeId;
+ // BEGIN EDIT - Fix hanging `INSERT INTO search_tmp_` queries
+ // Patch created by referencing http://bit.ly/2OcN8a4 and http://bit.ly/2QN3p7d
+ $optimizedSelectQuery = new \Zend_Db_Expr('(SELECT `entity_id`, `store_id`, `attribute_id`, `value` FROM '
+ . $this->resourceConnection->getTableName('catalog_product_index_eav')
+ . sprintf(
+ ' WHERE `attribute_id`=%s AND `store_id`=%s'
+ . ' GROUP BY CONCAT(`entity_id`," ",`attribute_id`," ",`store_id`," ",`value`) )',
+ $attributeId,
+ $this->storeManager->getStore()->getId()
+ )
+ );
+
$select->joinInner(
- [$filterJoinAlias => $this->resourceConnection->getTableName('catalog_product_index_eav')],
+ [$filterJoinAlias => $optimizedSelectQuery],
+ // END EDIT
$this->conditionManager->combineQueries(
$this->getJoinConditions($attributeId, $mainTableAlias, $filterJoinAlias),
Select::SQL_AND
Index: Model/Search/FilterMapper/VisibilityFilter.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Model/Search/FilterMapper/VisibilityFilter.php (date 1574535765359)
+++ Model/Search/FilterMapper/VisibilityFilter.php (date 1574535765359)
@@ -118,9 +118,17 @@
private function applyFilterByJoin(FilterInterface $filter, Select $select)
{
$mainTableAlias = $this->extractTableAliasFromSelect($select);
+
+ // BEGIN EDIT - Fix hanging `INSERT INTO search_tmp_` queries
+ // Patch created by referencing http://bit.ly/2OcN8a4 and http://bit.ly/2QN3p7d
+ $optimizedVisibilityQuery = new \Zend_Db_Expr('(SELECT `entity_id`, `store_id`, `attribute_id`, `value` FROM '
+ . $this->resourceConnection->getTableName('catalog_product_index_eav')
+ . sprintf(' WHERE `attribute_id`=%s AND `store_id`=%s GROUP BY CONCAT(`entity_id`," ",`attribute_id`," ",`store_id`," ",`value`) )', $this->getVisibilityAttributeId(), $this->storeManager->getStore()->getId())
+ );
$select->joinInner(
- ['visibility_filter' => $this->resourceConnection->getTableName('catalog_product_index_eav')],
+ ['visibility_filter' => $optimizedVisibilityQuery],
+ // END EDIT
$this->conditionManager->combineQueries(
[
sprintf('%s.entity_id = visibility_filter.entity_id', $mainTableAlias),
@randyruby
Copy link

I install this patch and it care of the 1205 error but now I main.CRITICAL: SQLSTATE[42S22]: Column not found: 1054 Unknown column

Every second in they system and exception logs

@erikhansen
Copy link
Author

The patch above is for Open Source. If you're using Commerce, try replacing instances of entity_id with row_id.

@randyruby
Copy link

randyruby commented Jun 29, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment