Re: Indexing problem with OFFSET LIMIT

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Merlin Moncure" <mmoncure(at)gmail(dot)com>
Cc: "Oliver Weichhold" <oliver(at)weichhold(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Indexing problem with OFFSET LIMIT
Date: 2008-08-30 02:58:34
Message-ID: 7115.1220065114@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Merlin Moncure" <mmoncure(at)gmail(dot)com> writes:
> On Fri, Aug 29, 2008 at 4:38 PM, Oliver Weichhold <oliver(at)weichhold(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ron Mayer 2008-08-30 03:08:03 Re: Postgresql 8.3 statistic stadistinct value dead wrong even with default_statistics_target=1000
Previous Message Bill Todd 2008-08-30 02:46:28 SELECT INTO returns incorrect values