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 to me with this:
Planning time: 0.286 ms
Execution time: 127.233 ms
Why is this? The difference is too big I think
I think you have a small misunderstanding. I'll try to describe what's going on when you run a query:
The EXPLAIN
command prints a description of that process. Now:
EXPLAIN
output is the time server spent on step 3 only.EXPLAIN
output is the time the server spent on step 2 only. I believe you can think of it as "the time the planner thinks the query will take", which we can call "planned [execution] time" or "estimated execution time".So there's no reason to expect the planning time and execution time to be similar. PostgreSQL wants to keep the planning time short to minimize its impact on the whole execution time.
You can find more information here in the manual.
Note that the execution time does not include the planning time. If you want a clear example of where the planning time alone is longer than the execution time, you can try the query explain analyse select 1
.