2 14.3. Controlling the Planner with Explicit JOIN Clauses #
4 It is possible to control the query planner to some extent by using the
5 explicit JOIN syntax. To see why this matters, we first need some
8 In a simple join query, such as:
9 SELECT * FROM a, b, c WHERE a.id = b.id AND b.ref = c.id;
11 the planner is free to join the given tables in any order. For example,
12 it could generate a query plan that joins A to B, using the WHERE
13 condition a.id = b.id, and then joins C to this joined table, using the
14 other WHERE condition. Or it could join B to C and then join A to that
15 result. Or it could join A to C and then join them with B — but that
16 would be inefficient, since the full Cartesian product of A and C would
17 have to be formed, there being no applicable condition in the WHERE
18 clause to allow optimization of the join. (All joins in the PostgreSQL
19 executor happen between two input tables, so it's necessary to build up
20 the result in one or another of these fashions.) The important point is
21 that these different join possibilities give semantically equivalent
22 results but might have hugely different execution costs. Therefore, the
23 planner will explore all of them to try to find the most efficient
26 When a query only involves two or three tables, there aren't many join
27 orders to worry about. But the number of possible join orders grows
28 exponentially as the number of tables expands. Beyond ten or so input
29 tables it's no longer practical to do an exhaustive search of all the
30 possibilities, and even for six or seven tables planning might take an
31 annoyingly long time. When there are too many input tables, the
32 PostgreSQL planner will switch from exhaustive search to a genetic
33 probabilistic search through a limited number of possibilities. (The
34 switch-over threshold is set by the geqo_threshold run-time parameter.)
35 The genetic search takes less time, but it won't necessarily find the
38 When the query involves outer joins, the planner has less freedom than
39 it does for plain (inner) joins. For example, consider:
40 SELECT * FROM a LEFT JOIN (b JOIN c ON (b.ref = c.id)) ON (a.id = b.id);
42 Although this query's restrictions are superficially similar to the
43 previous example, the semantics are different because a row must be
44 emitted for each row of A that has no matching row in the join of B and
45 C. Therefore the planner has no choice of join order here: it must join
46 B to C and then join A to that result. Accordingly, this query takes
47 less time to plan than the previous query. In other cases, the planner
48 might be able to determine that more than one join order is safe. For
50 SELECT * FROM a LEFT JOIN b ON (a.bid = b.id) LEFT JOIN c ON (a.cid = c.id);
52 it is valid to join A to either B or C first. Currently, only FULL JOIN
53 completely constrains the join order. Most practical cases involving
54 LEFT JOIN or RIGHT JOIN can be rearranged to some extent.
56 Explicit inner join syntax (INNER JOIN, CROSS JOIN, or unadorned JOIN)
57 is semantically the same as listing the input relations in FROM, so it
58 does not constrain the join order.
60 Even though most kinds of JOIN don't completely constrain the join
61 order, it is possible to instruct the PostgreSQL query planner to treat
62 all JOIN clauses as constraining the join order anyway. For example,
63 these three queries are logically equivalent:
64 SELECT * FROM a, b, c WHERE a.id = b.id AND b.ref = c.id;
65 SELECT * FROM a CROSS JOIN b CROSS JOIN c WHERE a.id = b.id AND b.ref = c.id;
66 SELECT * FROM a JOIN (b JOIN c ON (b.ref = c.id)) ON (a.id = b.id);
68 But if we tell the planner to honor the JOIN order, the second and
69 third take less time to plan than the first. This effect is not worth
70 worrying about for only three tables, but it can be a lifesaver with
73 To force the planner to follow the join order laid out by explicit
74 JOINs, set the join_collapse_limit run-time parameter to 1. (Other
75 possible values are discussed below.)
77 You do not need to constrain the join order completely in order to cut
78 search time, because it's OK to use JOIN operators within items of a
79 plain FROM list. For example, consider:
80 SELECT * FROM a CROSS JOIN b, c, d, e WHERE ...;
82 With join_collapse_limit = 1, this forces the planner to join A to B
83 before joining them to other tables, but doesn't constrain its choices
84 otherwise. In this example, the number of possible join orders is
85 reduced by a factor of 5.
87 Constraining the planner's search in this way is a useful technique
88 both for reducing planning time and for directing the planner to a good
89 query plan. If the planner chooses a bad join order by default, you can
90 force it to choose a better order via JOIN syntax — assuming that you
91 know of a better order, that is. Experimentation is recommended.
93 A closely related issue that affects planning time is collapsing of
94 subqueries into their parent query. For example, consider:
97 (SELECT * FROM a, b, c WHERE something) AS ss
100 This situation might arise from use of a view that contains a join; the
101 view's SELECT rule will be inserted in place of the view reference,
102 yielding a query much like the above. Normally, the planner will try to
103 collapse the subquery into the parent, yielding:
104 SELECT * FROM x, y, a, b, c WHERE something AND somethingelse;
106 This usually results in a better plan than planning the subquery
107 separately. (For example, the outer WHERE conditions might be such that
108 joining X to A first eliminates many rows of A, thus avoiding the need
109 to form the full logical output of the subquery.) But at the same time,
110 we have increased the planning time; here, we have a five-way join
111 problem replacing two separate three-way join problems. Because of the
112 exponential growth of the number of possibilities, this makes a big
113 difference. The planner tries to avoid getting stuck in huge join
114 search problems by not collapsing a subquery if more than
115 from_collapse_limit FROM items would result in the parent query. You
116 can trade off planning time against quality of plan by adjusting this
117 run-time parameter up or down.
119 from_collapse_limit and join_collapse_limit are similarly named because
120 they do almost the same thing: one controls when the planner will
121 “flatten out” subqueries, and the other controls when it will flatten
122 out explicit joins. Typically you would either set join_collapse_limit
123 equal to from_collapse_limit (so that explicit joins and subqueries act
124 similarly) or set join_collapse_limit to 1 (if you want to control join
125 order with explicit joins). But you might set them differently if you
126 are trying to fine-tune the trade-off between planning time and run