"Merlin Moncure" <mmoncure@xxxxxxxxx> writes: > On Fri, Aug 29, 2008 at 4:38 PM, Oliver Weichhold <oliver@xxxxxxxxxxxxx> 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