Re: Providing an alternative result when there is no result

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: Joshua Berry <yoberi(at)gmail(dot)com>, Postgresql General Mailing list <pgsql-general(at)postgresql(dot)org>
Subject: Re: Providing an alternative result when there is no result
Date: 2009-05-19 23:00:23
Message-ID: 19550.1242774023@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Merlin Moncure <mmoncure(at)gmail(dot)com> writes:
> On Mon, May 18, 2009 at 3:13 PM, Joshua Berry <yoberi(at)gmail(dot)com> wrote:
>> Is there an easy and efficient way to return a boolean false for a query
>> that returns no result, and true for one that does return a result?

> Probably the best general approach is to:

> select count(*) = 1 from
> (
> <query> limit 1
> )q;

Seems like EXISTS() is much more straightforward ...

> the point being that in some cases (not all obviously) the limit 1 can
> be a huge win, as you only care if there are rows or not.

... the system does know about optimizing EXISTS as if it were a LIMIT
query; you don't need to tack that on yourself.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jeffrey Trimble 2009-05-19 23:08:10 Re: Configure fails to find readline libraries
Previous Message Scott Mead 2009-05-19 22:56:12 Re: Configure fails to find readline libraries