To check if this is happening to you, you can profile your query by executing the following:
SET profiling = 1; *your query* SHOW PROFILE FOR QUERY 1;
(If you using phpMyAdmin you might need to add "SHOW PROFILE;" as well at the end)
This will show you a table of execution times for each phase in the execution of the query. The time taken by the query optimiser is in the "statistics" row. If this is too high, then you need to stop your optimiser from spending so much time.
Controlling how much time the optimiser should spend doing its job can be accomplished using the optimizer_search_depth variable. This is the maximum depth to search (presumably in some tree search algorithm) when optimising the query and is unfortunately set to the highest value by default. Being set to the highest value makes it the most likely to find the best execution plan but may also be unnecessarily time consuming. Thankfully there is a better default value to use: 0. When this variable is set to zero, the optimiser automatically picks a value based on the query and that seems to work well for me. To do this just execute the following query:
SET optimizer_search_depth = 0;
No comments:
Post a Comment