Re: Re: Does PostgreSQL support EXISTS?

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Michael Meskes <meskes(at)postgresql(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: Re: Does PostgreSQL support EXISTS?
Date: 2001-06-13 14:58:30
Message-ID: 200106131458.f5DEwVg07370@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> writes:
> >> On Wed, Jun 13, 2001 at 12:23:15PM +1000, Martijn van Oosterhout wrote:
> > select x from a where v in (select v from b)
> > select x from a where exists (select 1 from b where a.v = b.v)
> >>
> >> The latter should be faster than the former on every relational database
> >> system.
>
> > That surprises me because the subquery is a correlated subquery which
> > are usually slower on other databases that normal subqueries.
>
> However, the second form is easily able to make use of an index on b.v,
> whereas the first form is impossible to optimize unless you are able to
> rewrite it into some weird form of JOIN.

Assuming the index is there, yes, but again, do we use the index or run
the query. This has the same dynamics as index/heap scan and is
probably more complicated to figure out accurately.

> BTW, I just realized that the "weird form of JOIN" would have to be
> much stranger than I previously thought. The result of IN depends not
> only on whether the subselect's output has any matches to the current
> test value, but also on whether the subselect's output has any NULLs.
> So it's not simply a matter of doing a join with a special rule about
> producing no more than one output tuple per outer-query tuple. How
> would you check for the NULLs?

I thought NOT IN was the only one that was concerned about any NULL?

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thalis A. Kalfigopoulos 2001-06-13 14:59:30 Multiple pq_flush: send() failed: Broken pipe
Previous Message Bruno Wolff III 2001-06-13 14:57:02 Re: [SQL] ORDER BY what?