EXPLAIN / EXPLAIN ANALYZE: Shows the query plan (estimated). EXPLAIN ANALYZE actually runs the query and reports real timing and row counts. EXPLAIN shows how the database plans to run a query: which indexes it uses, how it joins tables and how many rows it expects to read. EXPLAIN ANALYZE actually runs the query and reports real times and row counts.
Frequently asked questions about EXPLAIN / EXPLAIN ANALYZE
How is EXPLAIN / EXPLAIN ANALYZE used in practice?
It is the first tool to reach for when a query is slow. The plan usually shows the cause straight away, such as a full table scan where you expected an index, or a join reading millions of rows.
What should you keep in mind with EXPLAIN / EXPLAIN ANALYZE?
Look for big gaps between estimated and actual row counts, scans on large tables, and filesorts on big result sets. Be careful with EXPLAIN ANALYZE on statements that change data, because it really executes them. Wrap those in a transaction you roll back. The plan for production data can differ from your laptop's, so test with realistic volumes.
What does an example of EXPLAIN / EXPLAIN ANALYZE look like?
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 42;
Master SQL query optimization, indexing strategies, and EXPLAIN analysis to dramatically improve database performance and eliminate slow queries. Use when debugging slow queries, designing database schemas, or optimizing application performance.
Postgres best practices maintained by Supabase, for Postgres running anywhere. Load this skill BEFORE writing or changing anything that lives in a Postgres database: creating or altering tables and columns (including choosing column types), schema design, migrations and declarative schema files, RLS policies and the tests that verify them, indexes, triggers, database functions, queues and schedule