Re: Why is the query not using the index for sorting? - Mailing list pgsql-performance

From Thom Brown
Subject Re: Why is the query not using the index for sorting?
Date
Msg-id bddc86150911220510rfb8f8a4jbdb90d7266cdf315@mail.gmail.com
Whole thread Raw
In response to Why is the query not using the index for sorting?  (Jonathan Blitz <jblitz@013.net>)
List pgsql-performance
2009/11/22 Jonathan Blitz <jblitz@013.net>
I have a table with a number of columns.
 
I perform
 
Select *
from table
order by a,b
 
There is an index on a,b which is clustered (as well as indexes on a and b alone).
I have issued the cluster and anyalze commands.
 
Nevertheless, PostgreSQL performs a Sequential Scan on the table and then performs a sort.
Am I missing something?
 
Jonathan Blitz

It depends on firstly the size of the table, and also the distribution of data in columns a and b.  If the stats for that table knows that the table has a natural order (i.e. they happen to be in roughly the order you've asked for them in), or the table isn't big enough to warrant using an index, then it won't bother using one.  It will pick whichever it believes to be the most efficient method.

Regards

Thom

pgsql-performance by date:

Previous
From: Jonathan Blitz
Date:
Subject: Why is the query not using the index for sorting?
Next
From: Craig Ringer
Date:
Subject: Re: Why is the query not using the index for sorting?