When to use EXPLAIN and EXPLAIN ANALYZE in PostgreSQL
PostgreSQL provides two commands that can be used to analyze a query plan: EXPLAIN
and EXPLAIN ANALYZE
.
EXPLAIN
EXPLAIN
is used to find the estimated cost of a query. It shows the steps that the PostgreSQL query optimizer plans to take in order to execute the query, and the estimated cost of each step.
EXPLAIN SELECT * FROM users;
EXPLAIN ANALYZE
EXPLAIN ANALYZE
does the same as EXPLAIN
, but it also executes the query and gives the actual results. This is useful if you want to know the actual cost of a query, instead of just an estimate.
EXPLAIN ANALYZE SELECT * FROM users;
Therefore, if you just want to know the estimated cost of a query, use EXPLAIN
. If you want to know the actual cost of a query, use EXPLAIN ANALYZE
.