Re: EXCEPT performace

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: "K(dot) Ari Krupnikov" <ari(at)cogsci(dot)ed(dot)ac(dot)uk>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: EXCEPT performace
Date: 2001-11-05 17:00:09
Message-ID: 20011105085659.I40711-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, 3 Nov 2001, K. Ari Krupnikov wrote:

> I noticed an unexpected difference in performance between seemingly equivalent queries:
>
> SELECT id FROM short_table EXCEPT SELECT id FROM long_table;
>
> and
>
> SELECT id FROM short_table EXCEPT
> (SELECT id FROM short_table JOIN long_table
> ON short_table.id = long_table.id);
>
> There is an index on longtable.id; short_table is a temporary table. EXPLAIN
> suggests that the index is ignored in the first case but consulted in the second.

A complete guess, but EXCEPT may be being rewritten into an IN<subquery>
style query, which would mean it'd have the same performance
issues as that form (see the faq).

If your queries are simple, it may be best to rewrite them as something
like:
select id from short_table where not exists (select id from long_table
where short_table.id=long_table.id);

There may be some null related issues because I don't remember how except
handles nulls, but that will probably run better.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2001-11-05 17:02:20 Re: request for advise
Previous Message Stephan Szabo 2001-11-05 16:56:57 Re: Referring a table in another database