codekingpro/portable-devtools
115k
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>14.3. Controlling the Planner with Explicit JOIN Clauses</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="planner-stats.html" title="14.2. Statistics Used by the Planner" /><link rel="next" href="populate.html" title="14.4. Populating a Database" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">14.3. Controlling the Planner with Explicit <code class="literal">JOIN</code> Clauses</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="planner-stats.html" title="14.2. Statistics Used by the Planner">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="performance-tips.html" title="Chapter 14. Performance Tips">Up</a></td><th width="60%" align="center">Chapter 14. Performance Tips</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="populate.html" title="14.4. Populating a Database">Next</a></td></tr></table><hr /></div><div class="sect1" id="EXPLICIT-JOINS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">14.3. Controlling the Planner with Explicit <code class="literal">JOIN</code> Clauses <a href="#EXPLICIT-JOINS" class="id_link">#</a></h2></div></div></div><a id="id-1.5.13.6.2" class="indexterm"></a><p>3 It is possible4 to control the query planner to some extent by using the explicit <code class="literal">JOIN</code>5 syntax. To see why this matters, we first need some background.6 </p><p>7 In a simple join query, such as:8</p><pre class="programlisting">9SELECT * FROM a, b, c WHERE a.id = b.id AND b.ref = c.id;10</pre><p>11 the planner is free to join the given tables in any order. For12 example, it could generate a query plan that joins A to B, using13 the <code class="literal">WHERE</code> condition <code class="literal">a.id = b.id</code>, and then14 joins C to this joined table, using the other <code class="literal">WHERE</code>15 condition. Or it could join B to C and then join A to that result.16 Or it could join A to C and then join them with B — but that17 would be inefficient, since the full Cartesian product of A and C18 would have to be formed, there being no applicable condition in the19 <code class="literal">WHERE</code> clause to allow optimization of the join. (All20 joins in the <span class="productname">PostgreSQL</span> executor happen21 between two input tables, so it's necessary to build up the result22 in one or another of these fashions.) The important point is that23 these different join possibilities give semantically equivalent24 results but might have hugely different execution costs. Therefore,25 the planner will explore all of them to try to find the most26 efficient query plan.27 </p><p>28 When a query only involves two or three tables, there aren't many join29 orders to worry about. But the number of possible join orders grows30 exponentially as the number of tables expands. Beyond ten or so input31 tables it's no longer practical to do an exhaustive search of all the32 possibilities, and even for six or seven tables planning might take an33 annoyingly long time. When there are too many input tables, the34 <span class="productname">PostgreSQL</span> planner will switch from exhaustive35 search to a <em class="firstterm">genetic</em> probabilistic search36 through a limited number of possibilities. (The switch-over threshold is37 set by the <a class="xref" href="runtime-config-query.html#GUC-GEQO-THRESHOLD">geqo_threshold</a> run-time38 parameter.)39 The genetic search takes less time, but it won't40 necessarily find the best possible plan.41 </p><p>42 When the query involves outer joins, the planner has less freedom43 than it does for plain (inner) joins. For example, consider:44</p><pre class="programlisting">45SELECT * FROM a LEFT JOIN (b JOIN c ON (b.ref = c.id)) ON (a.id = b.id);46</pre><p>47 Although this query's restrictions are superficially similar to the48 previous example, the semantics are different because a row must be49 emitted for each row of A that has no matching row in the join of B and C.50 Therefore the planner has no choice of join order here: it must join51 B to C and then join A to that result. Accordingly, this query takes52 less time to plan than the previous query. In other cases, the planner53 might be able to determine that more than one join order is safe.54 For example, given:55</p><pre class="programlisting">56SELECT * FROM a LEFT JOIN b ON (a.bid = b.id) LEFT JOIN c ON (a.cid = c.id);57</pre><p>58 it is valid to join A to either B or C first. Currently, only59 <code class="literal">FULL JOIN</code> completely constrains the join order. Most60 practical cases involving <code class="literal">LEFT JOIN</code> or <code class="literal">RIGHT JOIN</code>61 can be rearranged to some extent.62 </p><p>63 Explicit inner join syntax (<code class="literal">INNER JOIN</code>, <code class="literal">CROSS64 JOIN</code>, or unadorned <code class="literal">JOIN</code>) is semantically the same as65 listing the input relations in <code class="literal">FROM</code>, so it does not66 constrain the join order.67 </p><p>68 Even though most kinds of <code class="literal">JOIN</code> don't completely constrain69 the join order, it is possible to instruct the70 <span class="productname">PostgreSQL</span> query planner to treat all71 <code class="literal">JOIN</code> clauses as constraining the join order anyway.72 For example, these three queries are logically equivalent:73</p><pre class="programlisting">74SELECT * FROM a, b, c WHERE a.id = b.id AND b.ref = c.id;75SELECT * FROM a CROSS JOIN b CROSS JOIN c WHERE a.id = b.id AND b.ref = c.id;76SELECT * FROM a JOIN (b JOIN c ON (b.ref = c.id)) ON (a.id = b.id);77</pre><p>78 But if we tell the planner to honor the <code class="literal">JOIN</code> order,79 the second and third take less time to plan than the first. This effect80 is not worth worrying about for only three tables, but it can be a81 lifesaver with many tables.82 </p><p>83 To force the planner to follow the join order laid out by explicit84 <code class="literal">JOIN</code>s,85 set the <a class="xref" href="runtime-config-query.html#GUC-JOIN-COLLAPSE-LIMIT">join_collapse_limit</a> run-time parameter to 1.86 (Other possible values are discussed below.)87 </p><p>88 You do not need to constrain the join order completely in order to89 cut search time, because it's OK to use <code class="literal">JOIN</code> operators90 within items of a plain <code class="literal">FROM</code> list. For example, consider:91</p><pre class="programlisting">92SELECT * FROM a CROSS JOIN b, c, d, e WHERE ...;93</pre><p>94 With <code class="varname">join_collapse_limit</code> = 1, this95 forces the planner to join A to B before joining them to other tables,96 but doesn't constrain its choices otherwise. In this example, the97 number of possible join orders is reduced by a factor of 5.98 </p><p>99 Constraining the planner's search in this way is a useful technique100 both for reducing planning time and for directing the planner to a101 good query plan. If the planner chooses a bad join order by default,102 you can force it to choose a better order via <code class="literal">JOIN</code> syntax103 — assuming that you know of a better order, that is. Experimentation104 is recommended.105 </p><p>106 A closely related issue that affects planning time is collapsing of107 subqueries into their parent query. For example, consider:108</p><pre class="programlisting">109SELECT *110FROM x, y,111 (SELECT * FROM a, b, c WHERE something) AS ss112WHERE somethingelse;113</pre><p>114 This situation might arise from use of a view that contains a join;115 the view's <code class="literal">SELECT</code> rule will be inserted in place of the view116 reference, yielding a query much like the above. Normally, the planner117 will try to collapse the subquery into the parent, yielding:118</p><pre class="programlisting">119SELECT * FROM x, y, a, b, c WHERE something AND somethingelse;120</pre><p>121 This usually results in a better plan than planning the subquery122 separately. (For example, the outer <code class="literal">WHERE</code> conditions might be such that123 joining X to A first eliminates many rows of A, thus avoiding the need to124 form the full logical output of the subquery.) But at the same time,125 we have increased the planning time; here, we have a five-way join126 problem replacing two separate three-way join problems. Because of the127 exponential growth of the number of possibilities, this makes a big128 difference. The planner tries to avoid getting stuck in huge join search129 problems by not collapsing a subquery if more than <code class="varname">from_collapse_limit</code>130 <code class="literal">FROM</code> items would result in the parent131 query. You can trade off planning time against quality of plan by132 adjusting this run-time parameter up or down.133 </p><p>134 <a class="xref" href="runtime-config-query.html#GUC-FROM-COLLAPSE-LIMIT">from_collapse_limit</a> and <a class="xref" href="runtime-config-query.html#GUC-JOIN-COLLAPSE-LIMIT">join_collapse_limit</a>135 are similarly named because they do almost the same thing: one controls136 when the planner will <span class="quote">“<span class="quote">flatten out</span>”</span> subqueries, and the137 other controls when it will flatten out explicit joins. Typically138 you would either set <code class="varname">join_collapse_limit</code> equal to139 <code class="varname">from_collapse_limit</code> (so that explicit joins and subqueries140 act similarly) or set <code class="varname">join_collapse_limit</code> to 1 (if you want141 to control join order with explicit joins). But you might set them142 differently if you are trying to fine-tune the trade-off between planning143 time and run time.144 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="planner-stats.html" title="14.2. Statistics Used by the Planner">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="performance-tips.html" title="Chapter 14. Performance Tips">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="populate.html" title="14.4. Populating a Database">Next</a></td></tr><tr><td width="40%" align="left" valign="top">14.2. Statistics Used by the Planner </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 14.4. Populating a Database</td></tr></table></div></body></html>