Re: 8.3.1 query plan - Mailing list pgsql-general

From Tom Lane
Subject Re: 8.3.1 query plan
Date
Msg-id 10650.1219935678@sss.pgh.pa.us
Whole thread Raw
In response to Re: 8.3.1 query plan  (Steve Clark <sclark@netwolves.com>)
Responses Re: 8.3.1 query plan  (Steve Clark <sclark@netwolves.com>)
List pgsql-general
Steve Clark <sclark@netwolves.com> writes:
> Tom Lane wrote:
>> Consider testing the conditions on A at the top level, instead of
>> redundantly checking them inside the sub-query on B.

> Thanks for the response Tom, I am a SQL neophyte, so I'll try to
> rework the query.

What I meant to suggest was just

explain insert into myevents select * from t_unit_event_log a where
a.event_status = 1 and a.event_ref_log_no IS NOT NULL
and a.event_log_no not in (select event_log_no from myevents)
and exists (select b.event_log_no from myevents b
where a.event_ref_log_no = b.event_log_no)

ie, pull everything out of the subquery that doesn't depend on B.

Although, looking at it in this form, it seems like you'd be well
advised to then replace the EXISTS with an IN:

... and a.event_ref_log_no in (select b.event_log_no from myevents b)

Although those two forms should be equivalent, reality is that current
releases of PG are generally smarter about optimizing IN than EXISTS.
(The difference should largely go away as of 8.4.)

            regards, tom lane

pgsql-general by date:

Previous
From: "John T. Dow"
Date:
Subject: WAL file questions - how to relocate on Windows, how to replay after total loss, etc
Next
From: Adrian Klaver
Date:
Subject: Re: pg_dump problem