Re: Simple SQL Question

Поиск
Список
Период
Сортировка
От Greg Stark
Тема Re: Simple SQL Question
Дата
Msg-id 87zn1vl7bs.fsf@stark.xeocode.com
обсуждение исходный текст
Ответ на Re: Simple SQL Question  (Tomasz Myrta <jasiek@klaster.net>)
Список pgsql-sql
Tomasz Myrta <jasiek@klaster.net> writes:

> > select * from table1 LIMIT x
> > gives me the first x row of the result.
> > After that, I save the last value, and next time, I adjust
> > the query as
> > select * from table1 where itemkey>:lastvalue LIMIT x
> 
> Why do you complicate it so much? Everything you need is:
> 
> select * from table1 LIMIT x
> select * from table1 LIMIT x OFFSET x
> select * from table1 LIMIT x OFFSET 2*x
> 
> Remember to sort rows before using limit/offset.

There are two good reasons to prefer his Andras' solution to yours.

a) If the data is modified between the two queries his will continue from
where the previous page left off. Yours will either skip records or overlap
with the previous page.

b) If itemkey is indexed his will be an efficient index scan that performs
similarly regardless of what page is being fetched. Yours will perform more
and more slowly as the user gets deeper into the results.


Note that both queries are wrong however. You need an "ORDER BY itemkey" or
else nothing guarantees the second page has any relation at all to the first
page.

-- 
greg



В списке pgsql-sql по дате отправления:

Предыдущее
От: Ian Barwick
Дата:
Сообщение: Re: oracle v$session equivalent in postgresql
Следующее
От: T E Schmitz
Дата:
Сообщение: tricky GROUP BY / JOIN question