Before we start looking at how to optimize a query with the WHERE clause, you need to know the optimization recipe that you have to apply to be efficient.
Here is an optimization recipe:
- Identify the query that is slow
- Run an EXPLAIN plan on this slow query
- Identify what kind of slow problem your query is facing (for example, a full scan)
- Run a SHOW CREATE TABLE on the tables that are part of the slow query
- Identify whether the query uses the correct index and the order of the WHERE clause columns
- Make the required corrections or modifications
Before turning to an example from the recipe, I'd like to explain how an index works in MySQL so that you can get an idea of the impact on your queries by ...