When we want to optimize a query, we have to examine it by performing the following steps:
- Run the execution plan (EXPLAIN) on the query preceding your query with the word EXPLAIN
- Observe which part of your EXPLAIN can have the word ALL in the ACCESS_TYPE line or any other signs that your query needs attention
- Analyze the table structure whose query seems to have a performance problem with the SHOW CREATE TABLE [tablename] G command asking you the following questions:
- Is there at least one primary key?
- Is there an index on columns that you use in your WHERE, GROUP BY, ORDER BY, and HAVING clauses?
- If the answer to reference (b) is no, the next question would be: do I need a COMPOUND ...