Understanding the logic behind EXPLAIN ANALYSE
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
When processing queries, the database has two possibilities:
- Access the data directly using indexes.
- Perform sequential scan on disc or in memory.
Sequential scans on large tables are considered evil, because they eat database memory and CPU time. A sequential scan on 100.000 rows will consume 100.000 CPU cycles . Even more when scanning from disc.
Therefore, the logic behind debugging queries is to run EXPLAIN on each slow query and hunt for sequential scans.
When you find a sequential scan, try to rewrite the query.
Rewriting queries takes both time and effort.
Here are examples for the most simple cases:
- Add an index on columns involded in sequential scans.
- Make sure that JOIN left and right colums have indexes.
Example:
SELECT foo FROM table WHERE bar=1
Of course, bar should carry an index.
SELECT foo, bar FROM table1 t1
INNER JOIN table2 t2 on t1.a = t2.a
WHERE t2.c=1
Of course, t1.a and t2.a should carry indexes.
The guide covers detailed techniques for optimizing queries.
Usually, you can always transform a sequential query into a collection of INNER, LEFT and RIGHT JOINs which does not produce a sequential scan or produces it in the very last steps, when minimal information can be loaded in memory and computer fast.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion
Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life resources page to review all of your options.