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
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
Related Q & A:
- How can I force the login to a specific ip address?Best solution by Stack Overflow
- How to create a Restful web service in .Net Using MySQL?Best solution by stackoverflow.com
- How cookies are handled if you use a proxy between a client and server in HTTP?Best solution by Stack Overflow
- How can I email a photo on Yahoo email without using an attachment?Best solution by Yahoo! Answers
- How to use a wireless XBOX 360 controller without the wireless receiver in pc?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.