I make such a query
EXPLAIN (ANALYZE ,BUFFERS )
SELECT COUNT(id) q, day
FROM my_table
WHERE role_id && ARRAY[15, 17]
GROUP BY "day"
ORDER BY "day" DESC;
And Postgres responds me with this:
Planning time: 0.286 ms
Execution time: 127.233 ms
Why is this ? The difference is too big I think
In PostgreSQL, execution time for this query is 3.4 seconds, so optimization is required.
The execution plan shows how the table(s) referenced by the statement will be scanned — by plain sequential scan, index scan, etc. — and if multiple tables are referenced, what join algorithms will be used to bring together the required rows from each input table.
Some of the tricks we used to speed up SELECT-s in PostgreSQL: LEFT JOIN with redundant conditions, VALUES, extended statistics, primary key type conversion, CLUSTER, pg_hint_plan + bonus.
EXPLAIN ANALYZE will actually run the query, so be careful with updates or deletes! In those cases, consider not using ANALYZE, or you might perhaps wrap the entire statement in a transaction that you can roll back.
I think there's small misunderstanding of yours. I try to describe shortly what's going on when you run a query:
EXPLAIN
command prints you description of that process. Now:
EXPLAIN
output is time server spent on steps 3-4. EXPLAIN
output is time server spent on step 2 only. I believe you think of it as "time planner thinks that query would take", but that can be called "planned [execution] time" or "estimated execution time".So there's no reason why planning time and execution time difference should be smaller. PostgreSQL want to keep planning time short to minimize it's impact on whole execution time.
All is written here in manual.
Notice: Execution time not includes Planning time, you can try explain analyse select 1
to see a case where PlanningTime>ExecutionTime
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With