where is the index physically located in MySQL database?

How do you force MySQL to use a better-performing secondary index without using FORCE INDEX?

  • We recently converted our database from MyISAM to InnoDB. Now, when looking at some EXPLAINs, the optimizer chooses to use Key: PRIMARY, instead of the better-performing secondary index it was using in MyISAM. How long, if ever, will it take MySQL to wise up and use the secondary key?  I know I can FORCE INDEX, but it's a lot of painful code changes.

  • Answer:

    ANALYZE TABLE (http://dev.mysql.com/doc/refman/5.0/en/analyze-table.html) may do the trick. Note that it requires a read lock, so be prepared for downtime if the table is large.

Brian Rue at Quora Visit the source

Was this solution helpful to you?

Other answers

It's not necessarily going to be better-performing to use a secondary index over PRIMARY in InnoDB, even if it was better in MyISAM.  In MyISAM, any index lookup is approximately the same (primary vs. secondary).  In InnoDB, primary index lookups are significantly better than a secondary index lookup.  The optimizer could be making the correct choice. Do profile your queries; don't merely guess which query is faster. http://dev.mysql.com/doc/refman/5.5/en/show-profiles.html Do use ANALYZE TABLE as Brian Rue suggests, to make sure the optimizer's statistics are current.  But it doesn't matter how large the table is -- ANALYZE TABLE simply samples 8 random InnoDB pages in any case.  The time it takes is about the same time it takes to run SHOW TABLE STATUS, because both commands do the same sampling (with respect to InnoDB).

Bill Karwin

It's an interesting situation if you have Primary key in possible keys. That means it is involved in the filtering which as Bill said is very quick in InnoDB due to its storing mechanism (much faster than in MyISAM). MySQL tries its best to quickly return the results and in most cases it does a pretty good job with it. Have you tried to profile the query with and without FORCE INDEX? If you could paste the query and the table schema and the output of 'show indexes from table;' here we could say more about that. By the way you can override the sampling behaviour with innodb_stats_sample_pages which is a dinamyc parameter so you can actually increase it before the analyze table and change it back after it finished. http://dev.mysql.com/doc/refman/5.1/en/innodb-parameters.html#sysvar_innodb_stats_sample_pages

Károly Nagy

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.