Re: Speed or configuration - Mailing list pgsql-sql

From Tom Lane
Subject Re: Speed or configuration
Date
Msg-id 10681.966794490@sss.pgh.pa.us
Whole thread Raw
In response to Speed or configuration  ("Franz J Fortuny" <ffortuny@ivsol.com>)
List pgsql-sql
"Franz J Fortuny" <ffortuny@ivsol.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
tableYwhere 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.field1and 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


pgsql-sql by date:

Previous
From: The Hermit Hacker
Date:
Subject: Re: Speed or configuration
Next
From: Stephan Szabo
Date:
Subject: Re: Speed or configuration