pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Franz J Fortuny <ffortuny@ivsol.com>
Cc: pgSQL <pgsql-sql@postgresql.org>
Subject: Re: Speed or configuration
Date: Sun, 20 Aug 2000 14:01:30 -0400
Message-ID: <10681.966794490@sss.pgh.pa.us> (raw)
In-Reply-To: <010501c00ac0$72d47a20$0b01a8c0@ivsol>
References: <010501c00ac0$72d47a20$0b01a8c0@ivsol>

"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 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



view thread (9+ messages)  latest in thread

Message-ID: <10681.966794490@sss.pgh.pa.us>
Permalink:  ../10681.966794490@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/10681.966794490@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, ffortuny@ivsol.com
  Subject: Re: Speed or configuration
  In-Reply-To: <10681.966794490@sss.pgh.pa.us>
  References: <010501c00ac0$72d47a20$0b01a8c0@ivsol> <10681.966794490@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox