Re: LIMIT and UNION ALL

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Dave Johansen <davejohansen(at)gmail(dot)com>
Cc: pgsql-performance <pgsql-performance(at)postgresql(dot)org>
Subject: Re: LIMIT and UNION ALL
Date: 2011-05-18 15:59:47
Message-ID: BANLkTi=XBO_i3uwxOSjJAxPs_VG-U21egA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hello

2011/5/18 Dave Johansen <davejohansen(at)gmail(dot)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

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Scott Carey 2011-05-18 17:41:40 Re: hash semi join caused by "IN (select ...)"
Previous Message Robert Klemme 2011-05-18 15:54:47 Re: LIMIT and UNION ALL