Query performance can be affected by many things. Some of these can be manipulated by the user, while others are fundamental to the underlying design of the system. This chapter provides some hints about understanding and tuning PostgreSQL performance.
PostgreSQL devises a query plan for each query it is given. Choosing the right plan to match the query structure and the properties of the data is absolutely critical for good performance. You can use the EXPLAIN command to see what query plan the system creates for any query.
The numbers that are quoted by EXPLAIN are:
Estimated start-up cost (time expended before output scan can start; for example, the time to do the sorting in a SORT node).
Estimated total cost (if all tuples are retrieved, which they may not be, a query with a LIMIT will stop short of paying the total cost, for example).
Estimated number of rows output by this plan node (again, without regard for any LIMIT).
Estimated average width (in bytes) of rows output by this plan node.
The costs are measured in units of disk page fetches. (CPU effort estimates are converted into disk-page units using some fairly arbitrary fudge-factors.)
It is important to note that the cost of an upper-level node includes the cost of all its child nodes. It is also important to realize that the cost reflects only things that the planner/optimizer cares about. In particular, the cost does not consider the time spent transmitting result tuples to the frontend, which could be a pretty dominant factor in the true elapsed time, but the planner ignores it because it cannot change it by altering the plan. (Every correct plan will output the same tuple set.)
Rows output is more difficult because it is not the number of rows processed/scanned by the query. It is usually less, reflecting the estimated selectivity of any WHERE-clause constraints that are being applied at this node. Ideally the top-level rows estimate will approximate the number of rows actually returned, updated, or deleted by the query (again, without considering the effects of LIMIT).
Here are some examples (using the regress test database after a vacuum analyze, and almost-7.0 sources):
regression=# explain select * from tenk1; NOTICE: QUERY PLAN: Seq Scan on tenk1 (cost=0.00..333.00 rows=10000 width=148) |
This is a simple case. If you do
select * from pg_class where relname = 'tenk1'; |
Now modify the query to add a qualification clause:
regression=# explain select * from tenk1 where unique1 < 1000; NOTICE: QUERY PLAN: Seq Scan on tenk1 (cost=0.00..358.00 rows=1000 width=148) |
Modify the query to restrict the qualification even more:
regression=# explain select * from tenk1 where unique1 < 100; NOTICE: QUERY PLAN: Index Scan using tenk1_unique1 on tenk1 (cost=0.00..89.35 rows=100 width=148) |
Add another condition to the qualification:
regression=# explain select * from tenk1 where unique1 < 100 and regression-# stringu1 = 'xxx'; NOTICE: QUERY PLAN: Index Scan using tenk1_unique1 on tenk1 (cost=0.00..89.60 rows=1 width=148) |
Now try joining two tables, using the fields we have been discussing:
regression=# explain select * from tenk1 t1, tenk2 t2 where t1.unique1 < 100 regression-# and t1.unique2 = t2.unique2; NOTICE: QUERY PLAN: Nested Loop (cost=0.00..144.07 rows=100 width=296) -> Index Scan using tenk1_unique1 on tenk1 t1 (cost=0.00..89.35 rows=100 width=148) -> Index Scan using tenk2_unique2 on tenk2 t2 (cost=0.00..0.53 rows=1 width=148) |
In this nested-loop join, the outer scan is the same indexscan we had in the example before last, and so its cost and row count are the same because we are applying the "unique1 < 100" WHERE clause at that node. The "t1.unique2 = t2.unique2" clause is not relevant yet, so it does not affect the outer scan's row count. For the inner scan, the current outer-scan tuple's unique2 value is plugged into the inner indexscan to produce an indexqual such as "t2.unique2 = constant". Thus we get the same inner-scan plan and costs that we would get from, say, "explain select * from tenk2 where unique2 = 42". The loop node's costs are then set on the basis of the outer scan's cost, plus one repetition of the inner scan for each outer tuple (100 * 0.53, here), plus a little CPU time for join processing.
In this example the loop's output row count is the same as the product of the two scans' row counts, but that is not true in general. In general, you can have WHERE clauses that mention both relations and so can be applied only at the join point, not to either input scan. For example, if we added "WHERE ... AND t1.hundred < t2.hundred", that would decrease the output row count of the join node, but not change either input scan.
One way to look at variant plans is to force the planner to disregard whatever strategy it thought was the winner, using the enable/disable flags for each plan type. (This is a crude tool, but useful. See also the section "Controlling the Planner with Explicit JOINs".)
regression=# set enable_nestloop = off; SET VARIABLE regression=# explain select * from tenk1 t1, tenk2 t2 where t1.unique1 < 100 regression-# and t1.unique2 = t2.unique2; NOTICE: QUERY PLAN: Hash Join (cost=89.60..574.10 rows=100 width=296) -> Seq Scan on tenk2 t2 (cost=0.00..333.00 rows=10000 width=148) -> Hash (cost=89.35..89.35 rows=100 width=148) -> Index Scan using tenk1_unique1 on tenk1 t1 (cost=0.00..89.35 rows=100 width=148) |