Re: IN or EXISTS?? faster one

From: Manfred Koizar <mkoi-pg(at)aon(dot)at>
To: "Prachi Jain" <prachijain3(at)rediffmail(dot)com>
Cc: christoph(dot)dalitz(at)hs-niederrhein(dot)de, pgsql-general(at)postgresql(dot)org, shridhar_daithankar(at)persistent(dot)co(dot)in, tgl(at)sss(dot)pgh(dot)pa(dot)us
Subject: Re: IN or EXISTS?? faster one
Date: 2002-12-19 15:56:11
Message-ID: ibq30vcb6iso5o0u4069dt9e1lg5apjqpi@4ax.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 19 Dec 2002 13:09:47 -0000, "Prachi Jain"
<prachijain3(at)rediffmail(dot)com> wrote:
>I am using too many subqueries in my queries. I have read some
>FAQs that using EXISTS is faster than IN. Is that correct?? I
>tried to get the total runtime using EXPLAIN ANALYZE, but i got
>total runtime for the query with IN but not for the query with
>EXISTS.

What do you mean by "EXPLAIN ANALYZE ... not for the query with
EXISTS"? Was there an error?

>EXPLAIN ANALYZE Select * from bom where depot_id in ( SELECT
>depot_id from depot where company_name ='SOME' );
>
>EXPLAIN ANALYZE Select * from bom WHERE EXISTS ( SELECT depot_id
> from depot where company_name ='SOME' and depot.depot_id =
>bom.depot_id );

Assuming depot_id is unique in depot, you could also write

SELECT bom.*
FROM bom INNER JOIN depot d ON bom.depot_id=d.depot_id
WHERE d.company_name ='SOME';

or

SELECT bom.*
FROM bom, depot d
WHERE bom.depot_id = d.depot_id
AND d.company_name = 'SOME';

and let the query optimizer find a good plan.

Servus
Manfred

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Karl DeBisschop 2002-12-19 16:04:17 Re: Getting 2002-12-18T17:32:40-05:00 (ISO 8601) from
Previous Message Tom Lane 2002-12-19 15:50:51 Re: Getting 2002-12-18T17:32:40-05:00 (ISO 8601) from to_date()