Re: CREATE INDEX .. ON table1 (field1 asc, field2 desc)

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Bruno Wolff III <bruno(at)wolff(dot)to>
Cc: Timur <itvthor(at)sdf(dot)lonestar(dot)org>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: CREATE INDEX .. ON table1 (field1 asc, field2 desc)
Date: 2002-08-24 15:56:43
Message-ID: 20020824084559.R36889-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Sat, 24 Aug 2002, Bruno Wolff III wrote:

> On Sat, Aug 24, 2002 at 20:18:25 +0500,
> Timur <itvthor(at)sdf(dot)lonestar(dot)org> wrote:
> > Hello!
> >
> > I am new to PostgreSQL and databases in general, and I have this
> > question: why it is not possible to specify ordering when creating an
> > index?
> >
> > Let's say I have a query which looks like this:
> > SELECT * FROM table1
> > ORDER BY field1, field2 DESC
> > LIMIT 100;
> >
> > Apparently, system won't use index ON (field1, field2) ..
> >
> > I think it is pretty simple and hope there is a way to create index
> > with field2 indexed in descending order..
>
> Not that I could tell by looking at the documentation. The simplest way
> do get the effect would probably be to create a function that can be used
> to order the data. You can create an index on that function and use that
> function in order by clauses.
>
> Another approach that might work is to create a new operator class that
> will effectively sort data in reverse order. I don't know enough about
> creating operator classes to know for sure whether it would be possible
> or how hard it would be to do.

Well, in 7.3 it appears to be pretty easy, although it's probably tricky
in previous versions. I haven't tried it fully, but I needed to make
a simple function that did the comparison and set up the operators for
the opclass as the reverse of normal and explain shows it using the index
for a query like the above.

In previous versions you should be able to do the same but you need to
hack at system tables to do so (7.3 has a handy create operator class)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2002-08-24 16:34:44 Re: CREATE INDEX .. ON table1 (field1 asc, field2 desc)
Previous Message Bruno Wolff III 2002-08-24 15:42:46 Re: CREATE INDEX .. ON table1 (field1 asc, field2 desc)