Re: LEFT JOINs not optimized away when not needed

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Moshe Jacobson <moshe(at)neadwerx(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Postgres-Bugs <pgsql-bugs(at)postgresql(dot)org>, Robert Mowlavi <robert(at)neadwerx(dot)com>, Chris Autry <chris(at)neadwerx(dot)com>
Subject: Re: LEFT JOINs not optimized away when not needed
Date: 2014-07-10 15:06:49
Message-ID: CA+Tgmoay+3rZeSeKaqdWeD_3h+V2rBVaaS73rd36+CKq-axgfg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

On Wed, Jul 9, 2014 at 1:37 PM, Moshe Jacobson <moshe(at)neadwerx(dot)com> wrote:
> On Wed, Jul 9, 2014 at 1:33 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Right. Yeah, I was not that excited about its usefulness either.
>> However, the OP seems to think that it's a common optimization
>> (I wonder if he can cite chapter and verse on which other DBMSes
>> do it).
>
> I make no claim about its commonness, but I think this type of optimization
> is very useful when selecting a limited subset of the columns from a complex
> view, particularly where the output rows are expected to be distinct on the
> exact set of columns that are selected.

Hmm. So maybe something like this?

CREATE VIEW person_with_cars AS
SELECT p.id, p.full_name, p.something_else, array_agg(c.plate_number)
AS plate_numbers
FROM person p LEFT JOIN cars c ON p.id = c.person_id
GROUP BY p.id, p.full_name, p.something_else;

It's reasonable to hope that if the aggregated column isn't selected,
the join will get removed, but cars (person_id) is not unique.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Moshe Jacobson 2014-07-10 16:12:03 Re: LEFT JOINs not optimized away when not needed
Previous Message Noah Misch 2014-07-10 15:02:44 Re: Missing file versions for a bunch of dll/exe files in Windows builds

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2014-07-10 15:11:52 Re: FW: [postgres-discuss] Insert query hangs
Previous Message Robert Haas 2014-07-10 14:46:30 Re: Pg_upgrade and toast tables bug discovered