index in desc order

Started by AI Rummanover 15 years ago8 messagesgeneral
Jump to latest
#1AI Rumman
rummandba@gmail.com

Is it possible to create an index in descending order?

#2Sergey Konoplev
gray.ru@gmail.com
In reply to: AI Rumman (#1)
Re: index in desc order

On 2 November 2010 12:36, AI Rumman <rummandba@gmail.com> wrote:

Is it possible to create an index in descending order?

Yes it is - http://www.postgresql.org/docs/current/interactive/indexes-ordering.html

--
Sergey Konoplev

Blog: http://gray-hemp.blogspot.com /
Linkedin: http://ru.linkedin.com/in/grayhemp /
JID/GTalk: gray.ru@gmail.com / Skype: gray-hemp

#3Szymon Guz
mabewlun@gmail.com
In reply to: AI Rumman (#1)
Re: index in desc order

On 2 November 2010 10:36, AI Rumman <rummandba@gmail.com> wrote:

Is it possible to create an index in descending order?

yes...

create index i on t(i desc);

regards
Szymon

#4AI Rumman
rummandba@gmail.com
In reply to: Szymon Guz (#3)
Re: index in desc order

But I am using Postgresql 8.1. Is it possible here?

On Tue, Nov 2, 2010 at 3:42 PM, Szymon Guz <mabewlun@gmail.com> wrote:

Show quoted text

On 2 November 2010 10:36, AI Rumman <rummandba@gmail.com> wrote:

Is it possible to create an index in descending order?

yes...

create index i on t(i desc);

regards
Szymon

#5Sergey Konoplev
gray.ru@gmail.com
In reply to: AI Rumman (#4)
Re: index in desc order

On 2 November 2010 12:57, AI Rumman <rummandba@gmail.com> wrote:

But I am using Postgresql 8.1. Is it possible here?

I am afraid not. You could try to do the index using kind of 1/field
trick but I am not sure if it performs better than backward index scan
in general.

On Tue, Nov 2, 2010 at 3:42 PM, Szymon Guz <mabewlun@gmail.com> wrote:

On 2 November 2010 10:36, AI Rumman <rummandba@gmail.com> wrote:

Is it possible to create an index in descending order?

yes...
create index i on t(i desc);

regards
Szymon

--
Sergey Konoplev

Blog: http://gray-hemp.blogspot.com /
Linkedin: http://ru.linkedin.com/in/grayhemp /
JID/GTalk: gray.ru@gmail.com / Skype: gray-hemp

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Szymon Guz (#3)
Re: index in desc order

Szymon Guz <mabewlun@gmail.com> writes:

On 2 November 2010 10:36, AI Rumman <rummandba@gmail.com> wrote:

Is it possible to create an index in descending order?

create index i on t(i desc);

Note that there is actually no point at all in such a declaration.
The planner is perfectly capable of using backwards indexscans at
need, so the above index doesn't do anything you couldn't do with
a regular ascending-order index.

The cases where this feature is actually worth something is where
you have a multi-column index and you need different sort orders
for the components, for example

create index xy on t (x asc, y desc);

which could be used to satisfy SELECT ... ORDER BY x ASC, y DESC.

The OP didn't say what he wanted to use the feature for, but
unless it's something like that, there's probably a better way.

regards, tom lane

#7Michal Politowski
mpol+pg@meep.pl
In reply to: Tom Lane (#6)
Re: index in desc order

On Tue, 2 Nov 2010 10:10:19 -0400, Tom Lane wrote:

Szymon Guz <mabewlun@gmail.com> writes:

On 2 November 2010 10:36, AI Rumman <rummandba@gmail.com> wrote:

Is it possible to create an index in descending order?

create index i on t(i desc);

Note that there is actually no point at all in such a declaration.
The planner is perfectly capable of using backwards indexscans at
need, so the above index doesn't do anything you couldn't do with
a regular ascending-order index.

Cannot there be a (system/hardware) setup where there is a perceptible
performance difference between forward and backward index scans?

--
Michał Politowski
Talking has been known to lead to communication if practiced carelessly.

#8Dimitri Fontaine
dimitri@2ndQuadrant.fr
In reply to: Michal Politowski (#7)
Re: index in desc order

Michal Politowski <mpol+pg@meep.pl> writes:

Cannot there be a (system/hardware) setup where there is a perceptible
performance difference between forward and backward index scans?

I think it's been reported already that backward index scans indeed can
be much slower than forward index scan, but that how to model that is
still unclear and undone in the cost estimations.

You will have to crawl the pgsql-performance list yourself, though…

Regards,
--
Dimitri Fontaine
http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support