Skip site navigation (1) Skip section navigation (2)

Re: Strange query plan invloving a view

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Rich Doughty <rich(at)opusvl(dot)com>
Cc: Pgsql-Performance <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Strange query plan invloving a view
Date: 2005-11-17 18:06:55
Message-ID: 29531.1132250815@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-performance
Rich Doughty <rich(at)opusvl(dot)com> writes:
> However, the following query (which i believe should be equivalent)

> SELECT *
> FROM
> 	tokens.ta_tokenhist   h INNER JOIN
> 	tokens.ta_tokens      t ON h.token_id = t.token_id LEFT JOIN
> 	tokens.ta_tokenhist   i ON t.token_id = i.token_id AND
>                                     i.status   = 'issued'   LEFT JOIN
> 	tokens.ta_tokenhist   s ON t.token_id = s.token_id AND
>                                     s.status   = 'sold'     LEFT JOIN
> 	tokens.ta_tokenhist   r ON t.token_id = r.token_id AND
>                                     r.status   = 'redeemed'
> WHERE
> 	h.sarreport_id = 9
> ;

No, that's not equivalent at all, because the implicit parenthesization
is left-to-right; therefore you've injected the constraint to a few rows
of ta_tokenhist (and therefore only a few rows of ta_tokens) into the
bottom of the LEFT JOIN stack.  In the other case the constraint is at
the wrong end of the join stack, and so the full view output gets formed
before anything gets thrown away.

Some day the Postgres planner will probably be smart enough to rearrange
the join order despite the presence of outer joins ... but today is not
that day.

			regards, tom lane

In response to

Responses

pgsql-performance by date

Next:From: Alex TurnerDate: 2005-11-17 19:48:38
Subject: Re: Hardware/OS recommendations for large databases (
Previous:From: Guillaume SmetDate: 2005-11-17 17:47:09
Subject: weird performances problem

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group