Re: order by x DESC, y ASC indexing problem

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Vincent-Olivier Arsenault <vincent(at)up4c(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: order by x DESC, y ASC indexing problem
Date: 2002-10-04 03:14:43
Message-ID: 16871.1033701283@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Vincent-Olivier Arsenault <vincent(at)up4c(dot)com> writes:
> How to have the planner use an index in the case of a query like :
> SELECT * FROM TABLE1 ORDER BY X DESC, Y ASC;

A normal index on (X,Y) is useless for this query, because neither
scan direction in the index corresponds to the sort ordering you
are asking for.

In theory you could build a custom "reverse sort order" operator
class for X's datatype, and then make an index using the reverse
opclass for X and the normal opclass for Y. Or the other way
round (normal sort order for X and reverse for Y).

In practice, as Josh notes nearby, this is a waste of time for
the query as given: whole-table sorts usually are better done
by sorting not by indexscanning. If you are doing a partial scan
like
SELECT ... ORDER BY ... LIMIT some-small-number
then it might be worth the trouble to set up a custom-order index.

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Oliver Elphick 2002-10-04 05:03:13 Re: [SQL] [GENERAL] CURRENT_TIMESTAMP
Previous Message Bruno Wolff III 2002-10-04 03:12:11 Re: order by x DESC, y ASC indexing problem