quickest query to check a table for existance of a value in a field

From: Shane Wright <me(at)shanewright(dot)co(dot)uk>
To: pgsql-sql(at)postgresql(dot)org
Subject: quickest query to check a table for existance of a value in a field
Date: 2002-03-25 20:16:28
Message-ID: 200203252016.g2PKGVg16516@fullerruss.dsvr.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Thomas Swan 2002-03-25 20:52:17 Re: Cleaning up template 1
Previous Message Daniel Lundin 2002-03-25 20:16:25 Re: Declaring arrays in plpgsql functions