pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedquickest query to check a table for existance of a value in a field
3+ messages / 2 participants
[nested] [flat]
* quickest query to check a table for existance of a value in a field
@ 2002-03-25 20:16 Shane Wright <me@shanewright.co.uk>
0 siblings, 1 reply; 3+ messages in thread
From: Shane Wright @ 2002-03-25 20:16 UTC (permalink / raw)
To: pgsql-sql
Hi
I have a reasonable sized table (~2m rows), with an indexed field for which
there are ~200 possible values.
I want a quick way to find out if there are any rows matching a specified
value for this indexed field. I dont care how many there are or what their
properties are, just if there are any or not.
Here's a quick example:
CREATE TABLE mytable(c1 INT4 PRIMARY KEY, c2 INT2);
CREATE INDEX myindex ON mytable(c2);
SELECT COUNT(c2) AS num FROM mytable WHERE c2=1234;
The time it takes to execute the above query appears to be of the order O(n)
- is there an O(1) query to do it - given that I only want to know if num>0
or if num=0? (the function COUNT() doesn't have to be used).
Any help appreciated,
Thanks
--
Shane
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: quickest query to check a table for existance of a value in a field
@ 2002-03-25 21:41 chester c young <chestercyoung@yahoo.com>
parent: Shane Wright <me@shanewright.co.uk>
0 siblings, 1 reply; 3+ messages in thread
From: chester c young @ 2002-03-25 21:41 UTC (permalink / raw)
To: me@shanewright.co.uk; +Cc: pgsql-sql
> I want a quick way to find out if there are any rows matching a
> specified value for this indexed field. I dont care how many there
> are or what their properties are, just if there are any or not.
>
> SELECT COUNT(c2) AS num FROM mytable WHERE c2=1234;
>
> Thanks
> Shane
Try: select 1 as yes from mytable where c2=1234 limit 1;
or: select 1 as yes where exists( select 1 from mytable where c2=1234
);
would be interesting to see differences in performance on your data
__________________________________________________
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards®
http://movies.yahoo.com/
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: quickest query to check a table for existance of a value in a field
@ 2002-03-26 23:55 Shane Wright <me@shanewright.co.uk>
parent: chester c young <chestercyoung@yahoo.com>
0 siblings, 0 replies; 3+ messages in thread
From: Shane Wright @ 2002-03-26 23:55 UTC (permalink / raw)
To: pgsql-sql
Hi
Thanks, just tried this on my dev box (650,000 rows, ~50,000 per ID), the
original way (COUNT()) takes ~1/2 second (guestimating), while your way is as
instant as I can tel..
Beauty :)
--
Shane
On Monday 25 Mar 2002 9:41 pm, chester c young wrote:
> > I want a quick way to find out if there are any rows matching a
> > specified value for this indexed field. I dont care how many there
> > are or what their properties are, just if there are any or not.
> >
> > SELECT COUNT(c2) AS num FROM mytable WHERE c2=1234;
> >
> > Thanks
> > Shane
>
> Try: select 1 as yes from mytable where c2=1234 limit 1;
> or: select 1 as yes where exists( select 1 from mytable where c2=1234
> );
>
> would be interesting to see differences in performance on your data
>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Movies - coverage of the 74th Academy Awards®
> http://movies.yahoo.com/
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2002-03-26 23:55 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-03-25 20:16 quickest query to check a table for existance of a value in a field Shane Wright <me@shanewright.co.uk>
2002-03-25 21:41 ` chester c young <chestercyoung@yahoo.com>
2002-03-26 23:55 ` Shane Wright <me@shanewright.co.uk>
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