From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | James Cloos <cloos(at)jhcloos(dot)com> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: A more efficient way? |
Date: | 2010-10-31 14:10:47 |
Message-ID: | 19769.1288534247@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
James Cloos <cloos(at)jhcloos(dot)com> writes:
> I've a third-party app which is slower than it ought to be.
> It does one operation in particular by doing a query to get a list of
> rows, and then iterates though them to confirm whether it actually wants
> that row. As you can imagine that is very slow.
> This query gets the correct data set in one go, but is also slow:
> select p from m where s = 7 and p not in (select p from m where s != 7);
See if you can recast it as a NOT EXISTS. NOT IN is hard to optimize
because of its weird behavior with nulls.
If you're working with a less-than-current version of PG, you may
instead have to resort to a left-join-with-is-null locution, ie
select m.p from m left join
(select p from m where s != 7) m2
on (m.p=m2.p)
where m2.p is null and m.s = 7;
but it's hard to wrap your head around that sort of thing, so I'd
advise against using it if you can get decent performance with EXISTS.
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | Viktor Bojović | 2010-10-31 19:36:47 | Re: large xml database |
Previous Message | James Cloos | 2010-10-31 06:59:20 | A more efficient way? |