Re: LIMIT and UNION ALL

Поиск
Список
Период
Сортировка
От Robert Klemme
Тема Re: LIMIT and UNION ALL
Дата
Msg-id BANLkTimhKEfamSutk7HJdyVvosg7c_bpPw@mail.gmail.com
обсуждение исходный текст
Ответ на LIMIT and UNION ALL  (Dave Johansen <davejohansen@gmail.com>)
Ответы Re: LIMIT and UNION ALL  (Dave Johansen <davejohansen@gmail.com>)
Список pgsql-performance
On Wed, May 18, 2011 at 5:26 PM, Dave Johansen <davejohansen@gmail.com> wrote:
> I am using Postgres 8.3.3 and I have a VIEW which is a UNION ALL of two
> tables but when I do a select on the view using a LIMIT, it scans the entire
> tables and takes significantly longer than writing out the query with the
> LIMITs in the sub-queries themselves. Is there a solution to get the view to
> perform like the query with the LIMIT explicitly placed in the sub-queries?

Can you show DDL and queries?

The query with the LIMIT on the subqueries and the one with the LIMIT
on the overall query are not semantically equivalent.  Since you can
have an ORDER BY before the LIMIT on the query with the limit on the
view the database must have all the rows before it can apply the
ordering and properly determine the limit.  Although it might be
possible to determine under particular circumstances that only one of
the tables needs to be queried or tables need only be queried
partially I deem that quite complex. I do not know whether Postgres
can do such optimizations but for that we would certainly need to see
the concrete example (including constraint and indexes).

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

Предыдущее
От: Dave Johansen
Дата:
Сообщение: LIMIT and UNION ALL
Следующее
От: Pavel Stehule
Дата:
Сообщение: Re: LIMIT and UNION ALL