Re: improving a badly optimized query - Mailing list pgsql-general

From Tom Lane
Subject Re: improving a badly optimized query
Date
Msg-id 29070.1037775719@sss.pgh.pa.us
Whole thread Raw
In response to improving a badly optimized query  (Brandon Craig Rhodes <brandon@oit.gatech.edu>)
Responses Re: improving a badly optimized query  (Brandon Craig Rhodes <brandon@oit.gatech.edu>)
List pgsql-general
Brandon Craig Rhodes <brandon@oit.gatech.edu> writes:
> (a) (slow)
>         SELECT * FROM role_keys NATURAL LEFT JOIN role_person
>                 WHERE person = 28389;

> (b) (fast)
>         SELECT * FROM role_keys NATURAL JOIN role_person
>                 WHERE person = 28389;

> Apparently PostgreSQL does not realize that the rows created for
> unmatched role_keys rows by the LEFT JOIN are guaranteed to be thrown
> out by the WHERE clause (their `person' fields will be null).
> [ and hence the LEFT JOIN could be reduced to a JOIN ]

Hmm ... you are right, there is no such logic in there.  It seems like
a useful optimization, but I have an uncomfortable feeling that there's
something wrong with it.  Can you point to a rigorous proof that this is
okay in complicated contexts such as nested outer joins?

            regards, tom lane

pgsql-general by date:

Previous
From: Justin Clift
Date:
Subject: Re: Request for reference sites
Next
From: Christoph Dalitz
Date:
Subject: Re: [pgsql-general] Daily Digest V1 #2701