Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
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

pgsql-general by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group