Skip site navigation (1) Skip section navigation (2)

Re: in() VS exists()

From: "Bart Degryse" <Bart(dot)Degryse(at)indicator(dot)be>
To: "pgsql-sql" <pgsql-sql(at)postgresql(dot)org>,"Julien" <jcigar(at)ulb(dot)ac(dot)be>
Subject: Re: in() VS exists()
Date: 2008-03-13 13:46:14
Message-ID: (view raw or whole thread)
Lists: pgsql-sql
The chapter on indexes in the manual  ( )should give
you a pretty good idea on the why.
IN and EXISTS are not the only possibilities, you can also use inner or
outer joins.
Which solution performs best depends on the data, the database version,
the available indexes, ...

>>> Julien <jcigar(at)ulb(dot)ac(dot)be> 2008-03-13 15:47 >>>

Does anyone has an idea why sometimes:
- select ... where ... in (select ...)
is faster than :
- select ... where ... exists(select ...)
and sometimes it's the opposite ?

I had such a situation, I've pasted the queries on: and 

It's running PostgreSQL 8.1 with an effective_cache_size of 30000. is the primary key and there are indexes on
sequences(specimen_id) and specimen_measurements(specimen_id)

Is there a general "rule" to know when to use the in() version and
to use the exists() version ? Is it true to say that the exists()
version is more scalable (with many rows) than the in() version (from
the little tests I made it seems the case) ?


Julien Cigar
Belgian Biodiversity Platform ( )
Université Libre de Bruxelles (ULB)
Campus de la Plaine CP 257
Bâtiment NO, Bureau 4 N4 115C (Niveau 4)
Boulevard du Triomphe, entrée ULB 2
B-1050 Bruxelles
Mail: jcigar(at)ulb(dot)ac(dot)be 
Tel : 02 650 57 52

In response to


pgsql-sql by date

Next:From: Bart DegryseDate: 2008-03-13 14:12:49
Subject: Re: in() VS exists()
Previous:From: Dirk JagdmannDate: 2008-03-13 06:16:59
Subject: Re: Composite UNIQUE across two tables?

Privacy Policy | About PostgreSQL
Copyright © 1996-2015 The PostgreSQL Global Development Group