From: Stephan Szabo <sszabo@megazone23.bigpanda.com>
To: Alex Pilosov <alex@pilosoft.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: outer joins strangeness
Date: Sun, 23 Sep 2001 21:57:11 -0700 (PDT)
Message-ID: <Pine.BSF.4.21.0109232149160.3131-100000@megazone23.bigpanda.com> (raw)
In-Reply-To: <Pine.BSO.4.10.10109221458500.28103-100000@spider.pilosoft.com>
On Sun, 23 Sep 2001, Alex Pilosov wrote:
> It may be just me, or I am grossly misunderstanding syntax of outer joins,
> but I see that plans for my queries are different depending on how I place
> join conditions and sometimes even on order of the tables.
>
> Example:
> 1:
> explain select * from customers c,orders o left outer join adsl_orders ao
> on ao.order_id=o.order_id
> where c.cust_id=o.cust_id
> and c.cust_id=152
>
>
> Nested Loop (cost=94.23..577.47 rows=2 width=290)
> -> Index Scan using customers_pkey on customers c (cost=0.00..2.02
> rows=1 width=125)
> -> Materialize (cost=501.65..501.65 rows=5904 width=165)
> -> Hash Join (cost=94.23..501.65 rows=5904 width=165)
> -> Seq Scan on orders o (cost=0.00..131.04 rows=5904
> width=58)
> -> Hash (cost=86.18..86.18 rows=3218 width=107)
> -> Seq Scan on adsl_orders ao (cost=0.00..86.18
> rows=3218 width=107)
>
> Query 2:
>
> explain select * from customers c join orders o on c.cust_id=o.cust_id
> left outer join adsl_orders ao on ao.order_id=o.order_id
> where c.cust_id=152
>
> Nested Loop (cost=0.00..9.30 rows=2 width=290)
> -> Nested Loop (cost=0.00..5.06 rows=2 width=183)
> -> Index Scan using customers_pkey on customers c
> (cost=0.00..2.02 rows=1 width=125)
> -> Index Scan using orders_idx1 on orders o (cost=0.00..3.03
> rows=1 width=58)
> -> Index Scan using adsl_orders_pkey on adsl_orders ao
> (cost=0.00..2.02 rows=1 width=107)
>
> To me, both queries seem exactly identical in meaning, and should generate
> the same plans. However, in my experience, if I use outer join anywhere in
> the query, I must use "JOIN" syntax to join all other tables as well,
> otherwise, my query plans are _extremely_ slow.
Postgres treats join syntax as an explicit definition of what order to
joins in. So, I'd guess it sees the first as: do the LOJ and then join
that to the separate table.
And for right outer join (for example), those two queries would not
be equivalent if I read the ordering correctly. The former syntax
would mean outer first and then the inner, whereas the second would
be inner first then the outer, and that could have different results.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: sszabo@megazone23.bigpanda.com, alex@pilosoft.com
Subject: Re: outer joins strangeness
In-Reply-To: <Pine.BSF.4.21.0109232149160.3131-100000@megazone23.bigpanda.com>
References: <Pine.BSO.4.10.10109221458500.28103-100000@spider.pilosoft.com> <Pine.BSF.4.21.0109232149160.3131-100000@megazone23.bigpanda.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox