1
1
<!--
2
- $PostgreSQL: pgsql/doc/src/sgml/perform.sgml,v 1.54 2005/11/04 23:14:00 petere Exp $
2
+ $PostgreSQL: pgsql/doc/src/sgml/perform.sgml,v 1.55 2005/12/20 02:30:35 tgl Exp $
3
3
-->
4
4
5
5
<chapter id="performance-tips">
@@ -627,7 +627,7 @@ SELECT * FROM a, b, c WHERE a.id = b.id AND b.ref = c.id;
627
627
</para>
628
628
629
629
<para>
630
- When the query involves outer joins, the planner has much less freedom
630
+ When the query involves outer joins, the planner has less freedom
631
631
than it does for plain (inner) joins. For example, consider
632
632
<programlisting>
633
633
SELECT * FROM a LEFT JOIN (b JOIN c ON (b.ref = c.id)) ON (a.id = b.id);
@@ -637,16 +637,30 @@ SELECT * FROM a LEFT JOIN (b JOIN c ON (b.ref = c.id)) ON (a.id = b.id);
637
637
emitted for each row of A that has no matching row in the join of B and C.
638
638
Therefore the planner has no choice of join order here: it must join
639
639
B to C and then join A to that result. Accordingly, this query takes
640
- less time to plan than the previous query.
640
+ less time to plan than the previous query. In other cases, the planner
641
+ may be able to determine that more than one join order is safe.
642
+ For example, given
643
+ <programlisting>
644
+ SELECT * FROM a LEFT JOIN b ON (a.bid = b.id) LEFT JOIN c ON (a.cid = c.id);
645
+ </programlisting>
646
+ it is valid to join A to either B or C first. Currently, only
647
+ <literal>FULL JOIN</> completely constrains the join order. Most
648
+ practical cases involving <literal>LEFT JOIN</> or <literal>RIGHT JOIN</>
649
+ can be rearranged to some extent.
641
650
</para>
642
651
643
652
<para>
644
653
Explicit inner join syntax (<literal>INNER JOIN</>, <literal>CROSS
645
654
JOIN</>, or unadorned <literal>JOIN</>) is semantically the same as
646
- listing the input relations in <literal>FROM</>, so it does not need to
647
- constrain the join order. But it is possible to instruct the
648
- <productname>PostgreSQL</productname> query planner to treat
649
- explicit inner <literal>JOIN</>s as constraining the join order anyway.
655
+ listing the input relations in <literal>FROM</>, so it does not
656
+ constrain the join order.
657
+ </para>
658
+
659
+ <para>
660
+ Even though most kinds of <literal>JOIN</> don't completely constrain
661
+ the join order, it is possible to instruct the
662
+ <productname>PostgreSQL</productname> query planner to treat all
663
+ <literal>JOIN</> clauses as constraining the join order anyway.
650
664
For example, these three queries are logically equivalent:
651
665
<programlisting>
652
666
SELECT * FROM a, b, c WHERE a.id = b.id AND b.ref = c.id;
@@ -660,7 +674,8 @@ SELECT * FROM a JOIN (b JOIN c ON (b.ref = c.id)) ON (a.id = b.id);
660
674
</para>
661
675
662
676
<para>
663
- To force the planner to follow the <literal>JOIN</> order for inner joins,
677
+ To force the planner to follow the join order laid out by explicit
678
+ <literal>JOIN</>s,
664
679
set the <xref linkend="guc-join-collapse-limit"> run-time parameter to 1.
665
680
(Other possible values are discussed below.)
666
681
</para>
@@ -697,9 +712,9 @@ FROM x, y,
697
712
WHERE somethingelse;
698
713
</programlisting>
699
714
This situation might arise from use of a view that contains a join;
700
- the view's <literal>SELECT</> rule will be inserted in place of the view reference,
701
- yielding a query much like the above. Normally, the planner will try
702
- to collapse the subquery into the parent, yielding
715
+ the view's <literal>SELECT</> rule will be inserted in place of the view
716
+ reference, yielding a query much like the above. Normally, the planner
717
+ will try to collapse the subquery into the parent, yielding
703
718
<programlisting>
704
719
SELECT * FROM x, y, a, b, c WHERE something AND somethingelse;
705
720
</programlisting>
@@ -722,12 +737,12 @@ SELECT * FROM x, y, a, b, c WHERE something AND somethingelse;
722
737
linkend="guc-join-collapse-limit">
723
738
are similarly named because they do almost the same thing: one controls
724
739
when the planner will <quote>flatten out</> subselects, and the
725
- other controls when it will flatten out explicit inner joins. Typically
740
+ other controls when it will flatten out explicit joins. Typically
726
741
you would either set <varname>join_collapse_limit</> equal to
727
742
<varname>from_collapse_limit</> (so that explicit joins and subselects
728
743
act similarly) or set <varname>join_collapse_limit</> to 1 (if you want
729
744
to control join order with explicit joins). But you might set them
730
- differently if you are trying to fine-tune the trade off between planning
745
+ differently if you are trying to fine-tune the trade- off between planning
731
746
time and run time.
732
747
</para>
733
748
</sect1>
0 commit comments