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
Views: Raw Message | Whole Thread | Download mbox | Resend email
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

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2000-08-20 18:11:58 Re: Speed or configuration
Previous Message The Hermit Hacker 2000-08-20 17:59:48 Re: Speed or configuration