Lifting WHERE conditions out of inner select

From: "John D(dot) Burger" <john(at)mitre(dot)org>
To: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Lifting WHERE conditions out of inner select
Date: 2007-10-08 19:35:59
Message-ID: E7518800-8302-4988-A52B-AC8EABD649A2@mitre.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi -

A colleague presented the following very slow query to me:

SELECT DISTINCT lemma FROM word
JOIN sense USING (wordid)
JOIN synset USING (synsetid)
WHERE sense.synsetid
IN (SELECT synset2id FROM semlinkref
WHERE synset1id
IN (SELECT synsetid FROM sense
WHERE wordid = (SELECT wordid FROM word WHERE
lemma='scramble'))
AND linkid=1
AND synset.pos='v')
ORDER BY lemma;

I realized that the last constraint, synset.pos='v', actually applies
to one of the tables in the main join, and could be lifted out of the
double IN clause. Doing so sped the query up by a factor of 10,000.

My question is, should the planner have figured this out, and we're
just losing out because we're stuck in 7.4? Or is there some subtle
difference in semantics I'm missing? The select results were the
same in both cases, but I'm willing to believe that's an accident of
our data.

(Sorry if no one can answer my question without the table
definitions, etc. - it seemed worthwhile trying to get away without
that for now.)

Thanks.

- John D. Burger
MITRE

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Broersma Jr 2007-10-08 20:09:29 Re: Lifting WHERE conditions out of inner select
Previous Message A. Kretschmer 2007-10-08 19:20:34 Re: Request: Anyone using bogus / "humorous" X-Message-Flag headers, could we please turn them off