SQL/Query performance 9 min

Reading a query plan

Ask the engine what it intends to do before you optimise.

explainquery plantuning

EXPLAIN QUERY PLAN prints the strategy: which tables are scanned, which indexes are used and in what order tables are joined. Postgres and MySQL use EXPLAIN / EXPLAIN ANALYZE for the same thing.

sql · editable
loading editor…

Try it: Create an index on orders(customer_id) and run the plan again.

The rewrites that pay off most: stop wrapping filtered columns in functions, filter before joining rather than after, select only the columns you need, and replace a correlated subquery that runs per row with a single join or window function.

sql · editable
loading editor…

Measure before and after on realistic data volumes. On tiny tables a scan is often the fastest plan and every optimisation looks pointless.