Re: Damn slow query - Mailing list pgsql-hackers

From Joe Conway
Subject Re: Damn slow query
Date
Msg-id 3DA4A8EF.3040004@joeconway.com
Whole thread Raw
In response to Damn slow query  ("Magnus Naeslund(f)" <mag@fbab.net>)
List pgsql-hackers
Magnus Naeslund(f) wrote:
> Hello, i've got this query that's really slow...
> Figure this:
> 
> testdb=> select now() ; select gid from bs where gid not in ( select x
> from z2test ); select now();

"IN (subselect)" is notoriously slow (in fact it is an FAQ). Can you rewrite 
this as:

select b.gid from bs b where not exists (select 1 from z2test z where z.x = 
b.gid);

or possibly:

select b.gid from bs b left join z2test z on z.x = b.gid where z.x IS NULL;

HTH,

Joe



pgsql-hackers by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: Damn slow query
Next
From: "Magnus Naeslund(f)"
Date:
Subject: Re: Damn slow query