Re: OFFSET and subselects

Поиск
Список
Период
Сортировка
От Greg Stark
Тема Re: OFFSET and subselects
Дата
Msg-id 87ad6ffzmp.fsf@stark.dyndns.tv
обсуждение исходный текст
Ответ на OFFSET and subselects  (dbichko@genpathpharma.com (Dmitri Bichko))
Список pgsql-sql
dbichko@genpathpharma.com (Dmitri Bichko) writes:

> I am running in trouble with pagination here, somehow (rather naively) I
> assumed that when doing a LIMIT and OFFSET, the subselects on the records
> before the OFFSET would not be performed, which quite apparently is not the
> case. So, LIMIT 50 OFFSET 0 takes 100ms to run, LIMIT 50 OFFSET 50 takes
> 200ms, LIMIT 50 OFFSET 100 takes 300ms; and so forth, this really becomes
> unacceptable after a few pages.

If you don't need any of the results of the subqueries in your WHERE clause
then you can do this by introducing a view in your query like:

SELECT *,      (SELECT ...) AS sub_1,      (SELECT ...) AS sub_2,      (SELECT ...) AS sub_3 FROM (       SELECT x,y,z
      FROM ...        WHERE ...      )LIMIT 50
 
OFFSET 50


If you do use the results of the subqueries in your where clause or order by
clause then, well, you're SOL. Since the OFFSET and LIMIT clauses only kick in
after the where clause restrictions are taken into account.

-- 
greg



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

Предыдущее
От: "George A.J"
Дата:
Сообщение: Infinite loop crashes server
Следующее
От: ow
Дата:
Сообщение: Seq Scans when index expected to be used