pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stuart Brooks <stuartb@cat.co.za>
To: pgsql-sql@postgresql.org
Subject: Constraint on multicolumn index
Date: Fri, 10 Nov 2006 12:21:02 +0200
Message-ID: <021e01c704b1$ed75eec0$cc22a8c0@STU95> (raw)

Hi,

I am not sure this can be done but I'm trying to constrain a sorted set
efficiently using a multicolumn index in postgres. The (simplified)
scenario is this:

CREATE TABLE T
(
  a INT,
  b INT,
  c INT
);

CREATE INDEX t_idx ON T(a,b,c);

Now I can sort using t_idx:

 select * from T order by a,b,c;  -- all good, seq scan using t_idx


I can constrain on a single variable fine:

 select * from T where (a=10 AND b=100 AND c>1000) order by a,b,c;
 -- does seq scan on t_idx and uses the index in the constraint as
expected


But if I want the next item following t=(a=10,b=100,c=1000):

 select * from T
 where (a=10 AND b=100 AND c>1000) OR (a=10 AND b>100) OR (a>10)
 order by a,b,c;

then it just does an ordinary filter, and basically does a sequence scan
with no intelligence which isn't great if you've got a table of 20
million items.

Is there any way short of issuing 3 queries and joining them that I can
do this? I had hoped to be able to compare (a,b,c)>(10,100,1000) but of
course that evaluates to (a>10) and (b>100) and (c>1000). It feels like
there should be a simple solution to this... please help :)

Thanks
 Stuart





view thread (3+ messages)  latest in thread

Message-ID: <021e01c704b1$ed75eec0$cc22a8c0@STU95>
Permalink:  ../021e01c704b1$ed75eec0$cc22a8c0@STU95/
Also on:    postgresql.org/message-id/021e01c704b1$ed75eec0$cc22a8c0@STU95

 · 

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: stuartb@cat.co.za
  Subject: Re: Constraint on multicolumn index
  In-Reply-To: <021e01c704b1$ed75eec0$cc22a8c0@STU95>

* 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