pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Shane Wright <me@shanewright.co.uk>
To: pgsql-sql@postgresql.org
Subject: quickest query to check a table for existance of a value in a field
Date: Mon, 25 Mar 2002 20:16:28 +0000
Message-ID: <200203252016.g2PKGVg16516@fullerruss.dsvr.co.uk> (raw)

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



view thread (3+ messages)  latest in thread

Message-ID: <200203252016.g2PKGVg16516@fullerruss.dsvr.co.uk>
Permalink:  ../200203252016.g2PKGVg16516@fullerruss.dsvr.co.uk/
Also on:    postgresql.org/message-id/200203252016.g2PKGVg16516@fullerruss.dsvr.co.uk

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: me@shanewright.co.uk
  Subject: Re: quickest query to check a table for existance of a value in a field
  In-Reply-To: <200203252016.g2PKGVg16516@fullerruss.dsvr.co.uk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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