Re: LIMIT and UNION ALL - Mailing list pgsql-performance

From Robert Klemme
Subject Re: LIMIT and UNION ALL
Date
Msg-id BANLkTimhKEfamSutk7HJdyVvosg7c_bpPw@mail.gmail.com
Whole thread Raw
In response to LIMIT and UNION ALL  (Dave Johansen <davejohansen@gmail.com>)
Responses Re: LIMIT and UNION ALL  (Dave Johansen <davejohansen@gmail.com>)
List 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 by date:

Previous
From: Dave Johansen
Date:
Subject: LIMIT and UNION ALL
Next
From: Pavel Stehule
Date:
Subject: Re: LIMIT and UNION ALL