From: Bruce Momjian <maillist@candle.pha.pa.us>
To: Daniel Lopez <ridruejo@atm9.com.dtu.dk>
Cc: rison@biochemistry.ucl.ac.uk
Cc: tgl@sss.pgh.pa.us
Cc: pgsql-sql@hub.org
Subject: Re: [SQL] Subselect performance
Date: Tue, 28 Sep 1999 17:14:06 -0400 (EDT)
Message-ID: <199909282114.RAA04792@candle.pha.pa.us> (raw)
In-Reply-To: <199909212254.AAA13731@atm9.com.dtu.dk>
>
> > It is not your fault. We frequently get reports of this type, and the
> > behavior of the subquery is very non-intuitive. You would think that a
> > subquery and a join would have the same performance, but because of the
> > limitation of subqueries as being nested loop joined, this is not the
> > case, and subqueries are slower. We tell people to rewrite their query
> > as EXISTS, but by the time we tell them that, they have already spent
> > much time trying to figure out why the query is so slow, and I am sure
> > many people don't even know about the EXISTS workaround.
>
> You are right: I spend some time scratching my head, then some time
> searching the mailing lists and I finally made the query with a EXISTS,
> which works great for me :) Thanks
> Can this be a candidate to include in the FAQ?
>
> On the same idea, is there any good document out there with all the SQL
> "recipes" or common practice for things like : "Give me all the rows which
> have this value in this column more than once, etc"
> I do it with:
> select my_index, count(my_index) from my_table group by my_index having
> count(my_index) > 1;
>
> But this is a common query and would be interested in knowing which is the
> commonly accepted way of doing this
New FAQ:
4.23) Why are my subqueries using IN so slow?
Currently, we join subqueries to outer queries by sequential scanning
the result of the subquery for each row of the outer query. A workaround
is to replace IN with EXISTS. For example, change:
SELECT *
FROM tab
WHERE col1 IN (SELECT col2 FROM TAB2)
to:
SELECT *
FROM tab
WHERE EXISTS (SELECT col2 FROM TAB2 WHERE col1 = col2)
We hope to fix this limitation in a future release.
--
Bruce Momjian | http://www.op.net/~candle
maillist@candle.pha.pa.us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026
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: maillist@candle.pha.pa.us, ridruejo@atm9.com.dtu.dk, rison@biochemistry.ucl.ac.uk, tgl@sss.pgh.pa.us, pgsql-sql@hub.org
Subject: Re: [SQL] Subselect performance
In-Reply-To: <199909282114.RAA04792@candle.pha.pa.us>
References: <199909212254.AAA13731@atm9.com.dtu.dk> <199909282114.RAA04792@candle.pha.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