Sobes.tech
Junior — Middle

What is the difference between EXPLAIN and EXPLAIN ANALYZE commands in PostgreSQL in the context of query analysis?

sobes.tech AI

Answer from AI

The EXPLAIN command in PostgreSQL shows the execution plan of an SQL query, that is, how the database will execute it: which indexes to use, the order of operations, cost estimates, and row counts. This is a static analysis without actually executing the query.

The EXPLAIN ANALYZE command not only displays the plan but also executes the query, collecting real statistical data: how long each operation took, how many rows were processed. This helps understand how well the plan matches the actual execution and identify bottlenecks.

Example:

EXPLAIN SELECT * FROM users WHERE id = 1;
EXPLAIN ANALYZE SELECT * FROM users WHERE id = 1;

The first shows the plan, the second — the plan plus actual costs and execution time.