Skip to content

Instantly share code, notes, and snippets.

@parhamr
Last active December 25, 2015 17:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save parhamr/7011685 to your computer and use it in GitHub Desktop.
Save parhamr/7011685 to your computer and use it in GitHub Desktop.
MySQL 5.6.11 skips indices when given quoted INTs

Removing quotes from an INT column allows MySQL to use the index and this can reduce rows scanned by 99.997 percent. Example from Magento 1.12 on a production database; the query takes nearly a second to execute:

mysql> explain DELETE FROM `catalog_product_index_price` WHERE entity_id IN('433284', 433283)\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: catalog_product_index_price
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 773625
        Extra: Using where
1 row in set (0.00 sec)

This query is much faster:

mysql> explain DELETE FROM `catalog_product_index_price` WHERE entity_id IN(433284, 433283)\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: catalog_product_index_price
         type: range
possible_keys: PRIMARY,IDX_CATALOGPRODUCTINDEXPRICE_ENTITYID
          key: PRIMARY
      key_len: 4
          ref: NULL
         rows: 19
        Extra: Using where
1 row in set (0.00 sec)

Incorrectly quoting both INT values produces the same result:

mysql> explain DELETE FROM `catalog_product_index_price` WHERE entity_id IN('433284', '433283')\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: catalog_product_index_price
         type: range
possible_keys: PRIMARY,IDX_CATALOGPRODUCTINDEXPRICE_ENTITYID
          key: PRIMARY
      key_len: 4
          ref: NULL
         rows: 19
        Extra: Using where
1 row in set (0.00 sec)
@groggu
Copy link

groggu commented Nov 26, 2013

Ran into this today - it's locking up one of our customer's sites.

In class

Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract

find

public function reindexEntities($processIds)

and add

//turn all the items into ints
$processIds = array_map(intval,$processIds);

before

$adapter->beginTransaction();

@groggu
Copy link

groggu commented Nov 27, 2013

Seems to effect MySQL 5.1.x and up. version MySQL 5.0.95 uses the key correcty

@SchumacherFM
Copy link

In my approach I've eliminated the delete query completely without touching the Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract file or any other indexer related file.

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