Re: Indexing problem with OFFSET LIMIT - Mailing list pgsql-general

From Tom Lane
Subject Re: Indexing problem with OFFSET LIMIT
Date
Msg-id 7115.1220065114@sss.pgh.pa.us
Whole thread Raw
In response to Re: Indexing problem with OFFSET LIMIT  ("Merlin Moncure" <mmoncure@gmail.com>)
List pgsql-general
"Merlin Moncure" <mmoncure@gmail.com> writes:
> On Fri, Aug 29, 2008 at 4:38 PM, Oliver Weichhold <oliver@weichhold.com> wrote:
>> Consider this query:
>>
>> SELECT * FROM foo WHERE realm_id = 228 order by foo_name LIMIT 200 OFFSET
>> 15000

> try this:
> SELECT * FROM foo WHERE realm_id = 228 order by realm_id, foo_name
> LIMIT 200 OFFSET
>  15000

> Or even better don't use 'offset' at all.   It's simply lousy.   If
> you want to skip ahead 200 rows at a time, save off the previous last
> extracted rows in the app:

Yeah, large OFFSET values are always going to suck performance-wise,
because there is no magic way to skip over those rows --- the backend
has to read them, and then throw them away internally.  Avoid OFFSET.

Aside from the type of trick Merlin mentions, have you considered using
a cursor?

            regards, tom lane

pgsql-general by date:

Previous
From: Bill Todd
Date:
Subject: SELECT INTO returns incorrect values
Next
From: Ron Mayer
Date:
Subject: Re: Postgresql 8.3 statistic stadistinct value dead wrong even with default_statistics_target=1000