pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
aggregate functions, COUNT
4+ messages / 3 participants
[nested] [flat]

* aggregate functions, COUNT
@ 2001-10-02 16:11  Kevin Way <kevin.way@overtone.org>
  0 siblings, 1 reply; 4+ messages in thread

From: Kevin Way @ 2001-10-02 16:11 UTC (permalink / raw)
  To: pgsql-sql

I'm currently using a SELECT count(*) when all I really want to know is
if 1 or more records exist.  Is there a standard way to just find out if
a record exists?  If not, is there a way to avoid iterating over all the
records by writing an aggregate function?  Given what I've read of how
they work, I don't see how to make the function return before parsing
all the results anyway, am I wrong here?

Thanks for any help, or 2x4s,
Kevin Way

-- 
Kevin Way <kevin.way@overtone.org>
http://www.overtone.org/



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: aggregate functions, COUNT
@ 2001-10-02 16:47  Stephan Szabo <sszabo@megazone23.bigpanda.com>
  parent: Kevin Way <kevin.way@overtone.org>
  0 siblings, 1 reply; 4+ messages in thread

From: Stephan Szabo @ 2001-10-02 16:47 UTC (permalink / raw)
  To: Kevin Way <kevin.way@overtone.org>; +Cc: pgsql-sql

On Tue, 2 Oct 2001, Kevin Way wrote:

> I'm currently using a SELECT count(*) when all I really want to know is
> if 1 or more records exist.  Is there a standard way to just find out if
> a record exists?  If not, is there a way to avoid iterating over all the
> records by writing an aggregate function?  Given what I've read of how
> they work, I don't see how to make the function return before parsing
> all the results anyway, am I wrong here?

I think you could use EXISTS for that,
select EXISTS (<query>); should give a true/false on whether the 
query returned any rows.  I'm not sure if it stops after one row
or not, but if it doesn't you can add a limit 1 to the query.






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: aggregate functions, COUNT
@ 2001-10-02 17:59  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Stephan Szabo <sszabo@megazone23.bigpanda.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2001-10-02 17:59 UTC (permalink / raw)
  To: Stephan Szabo <sszabo@megazone23.bigpanda.com>; +Cc: Kevin Way <kevin.way@overtone.org>; pgsql-sql

Stephan Szabo <sszabo@megazone23.bigpanda.com> writes:
> I think you could use EXISTS for that,
> select EXISTS (<query>); should give a true/false on whether the 
> query returned any rows.  I'm not sure if it stops after one row
> or not, but if it doesn't you can add a limit 1 to the query.

Yes it does stop after one row; and furthermore, the planner knows to
generate a fast-start plan for it.  (Or at least it's supposed to
... hmm, this seems to be broken in current sources ...)  Anyway,
there's no need for LIMIT 1 inside an EXISTS, because the planner
assumes that automatically.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: aggregate functions, COUNT
@ 2001-10-03 02:54  Kevin Way <kevin.way@overtone.org>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 4+ messages in thread

From: Kevin Way @ 2001-10-03 02:54 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>

* Tom Lane <tgl@sss.pgh.pa.us> [02-10-01 18:02]:
> Stephan Szabo <sszabo@megazone23.bigpanda.com> writes:
> > I think you could use EXISTS for that,
> > select EXISTS (<query>); should give a true/false on whether the 
> > query returned any rows.  I'm not sure if it stops after one row
> > or not, but if it doesn't you can add a limit 1 to the query.
> 
> Yes it does stop after one row; and furthermore, the planner knows to
> generate a fast-start plan for it.  (Or at least it's supposed to
> ... hmm, this seems to be broken in current sources ...)  Anyway,
> there's no need for LIMIT 1 inside an EXISTS, because the planner
> assumes that automatically.

Thank you muchly, I did some profiling and SELECT EXISTS is indeed
exactly what I wanted.  

        -Kevin Way

-- 
Kevin Way <kevin.way@overtone.org>
http://www.overtone.org/



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2001-10-03 02:54 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-10-02 16:11 aggregate functions, COUNT Kevin Way <kevin.way@overtone.org>
2001-10-02 16:47 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>
2001-10-02 17:59   ` Tom Lane <tgl@sss.pgh.pa.us>
2001-10-03 02:54     ` Kevin Way <kevin.way@overtone.org>

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