Re: quickly getting the top N rows - Mailing list pgsql-performance

From Bill Moran
Subject Re: quickly getting the top N rows
Date
Msg-id 20071004141411.9509fe66.wmoran@collaborativefusion.com
Whole thread Raw
In response to quickly getting the top N rows  (Ben <bench@silentmedia.com>)
Responses Re: quickly getting the top N rows
List pgsql-performance
In response to Ben <bench@silentmedia.com>:

> If I have this:
>
> create table foo (bar int primary key);
>
> ...then in my ideal world, Postgres would be able to use that index on bar
> to help me with this:
>
> select bar from foo order by bar desc limit 20;
>
> But in my experience, PG8.2 is doing a full table scan on foo, then
> sorting it, then doing the limit. I have a more complex primary key, but I
> was hoping the same concept would still apply. Am I doing something wrong,
> or just expecting something that doesn't exist?

Show us the explain.

However, 2 guesses:
1) You never analyzed the table, thus PG has awful statistics and
   doesn't know how to pick a good plan.
2) You have so few rows in the table that a seq scan is actually
   faster than an index scan, which is why PG uses it instead.

--
Bill Moran
Collaborative Fusion Inc.
http://people.collaborativefusion.com/~wmoran/

wmoran@collaborativefusion.com
Phone: 412-422-3463x4023

pgsql-performance by date:

Previous
From: Mark Lewis
Date:
Subject: Re: quickly getting the top N rows
Next
From: Andreas Kretschmer
Date:
Subject: Re: quickly getting the top N rows