From: Pavel Stehule <pavel.stehule@gmail.com>
To: Dave Johansen <davejohansen@gmail.com>
Cc: pgsql-performance <pgsql-performance@postgresql.org>
Subject: Re: LIMIT and UNION ALL
Date: Wed, 18 May 2011 17:59:47 +0200
Message-ID: <BANLkTi=XBO_i3uwxOSjJAxPs_VG-U21egA@mail.gmail.com> (raw)
In-Reply-To: <BANLkTikw+DqY+8S+5icmzfbnAE9-13pZGQ@mail.gmail.com>
References: <BANLkTikw+DqY+8S+5icmzfbnAE9-13pZGQ@mail.gmail.com>
Hello
2011/5/18 Dave Johansen <davejohansen@gmail.com>:
> 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?
>
> I noticed a similar question in this post
> http://archives.postgresql.org/pgsql-general/2005-06/msg00488.php but I
> wasn't able to find an answer.
maybe
SELECT *
FROM (SELECT * FROM tab1 LIMIT n) s1
UNION ALL
SELECT *
FROM (SELECT * FROM tab2 LIMIT n) s2
LIMIT n
Regards
Pavel Stehule
>
> Thanks,
> Dave
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-performance@postgresql.org
Cc: pavel.stehule@gmail.com, davejohansen@gmail.com
Subject: Re: LIMIT and UNION ALL
In-Reply-To: <BANLkTi=XBO_i3uwxOSjJAxPs_VG-U21egA@mail.gmail.com>
References: <BANLkTikw+DqY+8S+5icmzfbnAE9-13pZGQ@mail.gmail.com> <BANLkTi=XBO_i3uwxOSjJAxPs_VG-U21egA@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox