Skip to main content
Albero Academy

Query Optimization Deep Dive

Make slow queries fast — read EXPLAIN properly, design indexes that actually get used, and find the queries worth fixing.

Chapter 13MySQL16 min readBeginner friendly
  • #MySQL
  • #Performance
  • #Indexes

Chapter 4 introduced indexes and EXPLAIN. This chapter is the deep version: reading a plan line by line, designing composite indexes that queries actually use, avoiding the patterns that silently disable them, and finding which queries are worth your time.

Reading EXPLAIN

The columns that matter most, and what you want to see:

ColumnGood sign
typeconst/eq_ref/ref/range — not ALL (full scan)
keythe index actually chosen (not NULL)
rowsa small estimate of rows examined
Extraavoid "Using filesort"/"Using temporary" on big data
the fuller picture
EXPLAIN ANALYZE
SELECT * FROM students WHERE programme_id = 3 AND package > 8
ORDER BY package DESC;
-- ANALYZE actually runs it and shows real timings, not just estimates.

Designing indexes

A composite index serves queries that filter its columns left-to-right. Order matters: put equality columns first, then the range/sort column:

an index the query can use
-- For: WHERE programme_id = ? AND package > ? ORDER BY package
CREATE INDEX idx_prog_pkg ON students(programme_id, package);
-- Equality (programme_id) first, then the range/sort column (package).
-- This one index handles the filter AND the sort — no filesort needed.

Index-killing patterns

  • Function on the column — WHERE YEAR(enrolled) = 2026 can't use an index on enrolled. Rewrite as a range: enrolled >= '2026-01-01' AND enrolled < '2027-01-01'.
  • Leading wildcard — LIKE '%term' can't use an index; LIKE 'term%' can.
  • Implicit type mismatch — comparing a string column to a number forces a scan.
  • OR across different columns — often defeats indexing; a UNION of two indexed queries can be faster.

Finding slow queries

Don't guess — measure. Enable the slow query log to capture the real offenders:

capture the slow ones
SET GLOBAL slow_query_log = ON;
SET GLOBAL long_query_time = 0.5; -- log anything over 0.5s
-- Then analyse the log (e.g. with mysqldumpslow) to find the top offenders.

Practice

Take a query filtering and sorting on two columns; add a composite index in the right order and confirm EXPLAIN drops the filesort. Rewrite a YEAR(col) = filter as a date range and watch the index get used. Enable the slow query log and find your slowest query.

Key takeaways

  1. 01In EXPLAIN want type≠ALL, a chosen key, low rows, and no filesort/temporary on big data.
  2. 02EXPLAIN ANALYZE runs the query and shows real timings, not just estimates.
  3. 03Composite index order: equality columns first, then the range/sort column; covering indexes answer from the index alone.
  4. 04Functions on columns, leading %-wildcards, type mismatches and cross-column OR all disable indexes.
  5. 05Use the slow query log to find the queries actually worth optimizing — measure, don't guess.