Skip site navigation (1) Skip section navigation (2)

Re: Speed or configuration

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Franz J Fortuny" <ffortuny(at)ivsol(dot)com>
Cc: "pgSQL" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Speed or configuration
Date: 2000-08-20 18:01:30
Message-ID: 10681.966794490@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-sql
"Franz J Fortuny" <ffortuny(at)ivsol(dot)com> writes:
> [ this query is slow: ]
> select xx1,xx2,xx3 from tableX
> where field1 in
> (select field1 from tableY where
> field2=NNN and field3=NNN2 and field4=NNN4)

What version of Postgres are you using, and what does EXPLAIN show
as the query plan for this query?  How many tableY rows is the sub-
query likely to produce, and how many matches do you expect to get
from tableX?

The Postgres planner is not currently very smart about optimizing
sub-SELECTs.  We hope to do something about that by 7.2 or so,
but in the meantime you should look at transforming the query to
some other form.  You might find that EXISTS will help:
  select xx1,xx2,xx3 from tableX
  where exists (select 1 from tableY where
  field1=tableX.field1 and field2=NNN and field3=NNN3 and field4=NNN4)
However this is unlikely to help much unless you change the index
structure for tableY.  Perhaps you could write it as a join:
  select xx1,xx2,xx3 from tableX, tableY
  where tableX.field1=tableY.field1 and field2=NNN
    and field3=NNN3 and field4=NNN4
although this won't give quite the same results if there can be multiple
matching rows in tableY for a tableX row.  (DISTINCT might help if so.)

			regards, tom lane

In response to

Responses

pgsql-sql by date

Next:From: Stephan SzaboDate: 2000-08-20 18:11:58
Subject: Re: Speed or configuration
Previous:From: The Hermit HackerDate: 2000-08-20 17:59:48
Subject: Re: Speed or configuration

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group