Re: Why is this query running slowly?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Yang Zhang <yanghatespam(at)gmail(dot)com>
Cc: Tomas Vondra <tv(at)fuzzy(dot)cz>, pgsql-general(at)postgresql(dot)org
Subject: Re: Why is this query running slowly?
Date: 2011-09-15 14:26:17
Message-ID: 10696.1316096777@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Yang Zhang <yanghatespam(at)gmail(dot)com> writes:
> On Thu, Sep 15, 2011 at 1:22 AM, Tomas Vondra <tv(at)fuzzy(dot)cz> wrote:
>> [ use a left join instead of NOT IN ]

> This worked great, thank you. Too bad the planner isn't smart enough
> to do this yet!

It never will be, because they're not equivalent queries. NOT IN has
different (and very hard-to-optimize) behavior in the face of NULLs in
the right-hand table.

The moral of the story is avoid NOT IN. You could use NOT EXISTS,
which is less of a notational change than recasting as a left join.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Harald Fuchs 2011-09-15 14:30:17 PostgreSQL 9.1.0 bug?
Previous Message Harald Fuchs 2011-09-15 14:14:59 Re: Bit datatype performance?