11
Single Query Profile
EXPLAIN ANALYZE
subtasks correspond to plan nodes
Duration – actual time or page I/O – buffers
Execution count – loops
Features
Besides the most resource-intensive nodes, optimization candidates are those
with a large cardinality estimation error.
Any change may result in a complete overhaul of the execution plan
Sometimes you have to settle for a basic EXPLAIN
Either way, we identify the query to optimize from among the executed ones.
How to Work with the Query Itself? The EXPLAIN ANALYZE command also
provides a detailed execution profile.
The subtasks of this profile are the plan's nodes (the plan isn't a flat list but a
tree). The execution duration of a node and the number of its repetitions will
display the "time" and "loops" values from the actual section. Using the
buffers parameter, you can get the I/O volume (and the time spent on I/O
operations if the track_io_timin parameter is enabled).
The execution plan also contains critical information about the optimizer's
expected cardinality for each step. Typically, if there's no significant error in
the cardinality estimate, the plan will be appropriate (if not, you should adjust
the global settings). Therefore, you should focus not only on the most
resource-intensive nodes but also on those with a substantial (an order of
magnitude or more) difference between the estimated "rows" and actual
"rows". Examine the most deeply nested problematic node, as the error will
propagate upward through the tree from there.
There are cases where a query runs so long that EXPLAIN ANALYZE can't
be executed. In such cases, you'll have to make do with a basic EXPLAIN
and try to determine the cause of inefficiency without complete information.
Working with large execution plans in text format isn't always convenient.
For better clarity, consider using third-party tools, such as