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),
