patternsqlMinor
Does MySQL exectution plan depend on available indexes?
Viewed 0 times
availableindexesplanexectutionmysqldoesdepend
Problem
Does MySQL choose an execution plan for a given query taking into account what indexes are available, or does it first choose an execution plan and then uses indexes if they are available?
Motivation: I want to decide which indexes would be useful. I have a number of typical queries. So I can imagine two possible strategies:
My DB is not very large, so I can afford playing with indexes. I currently use InnoDB, but I can switch to MyISAM or another if needed.
Motivation: I want to decide which indexes would be useful. I have a number of typical queries. So I can imagine two possible strategies:
- If the plans do not depend on available indexes: see the execution plans of those queries and add indexes useful for these plans, or
- If the plans depend on available indexes: add all possible indexes (a lot!), see the plans for the queries, and remove unused indexes.
My DB is not very large, so I can afford playing with indexes. I currently use InnoDB, but I can switch to MyISAM or another if needed.
Solution
MySQL's optimizer looks only at what indexes are available.
There is an exception in 5.6: If you have a subqueries such as
InnoDB is getting the most attention these days. MyISAM does not have any tricks up its sleeve.
Try out my Cookbook for creating the best index, given a SELECT. It may save you some false starts.
There is an exception in 5.6: If you have a subqueries such as
FROM ( SELECT ... ) JOIN ( SELECT ... ) ..., there are no indexes on the temporary tables that are created. This used to lead to terrible performance. Now, the optimizer will try out various indexes, and create the best one for the tmp table. I suspect it only looks for single-column indexes, not "compound" indexesInnoDB is getting the most attention these days. MyISAM does not have any tricks up its sleeve.
Try out my Cookbook for creating the best index, given a SELECT. It may save you some false starts.
Context
StackExchange Database Administrators Q#95854, answer score: 4
Revisions (0)
No revisions yet.