From: Tom Lane <tgl@sss.pgh.pa.us>
To: Paolo Losi <paolo.losi@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: pushing order by + limit to union subqueries
Date: Sat, 28 Feb 2015 11:24:01 -0500
Message-ID: <28519.1425140641@sss.pgh.pa.us> (raw)
In-Reply-To: <CAP=2L=FRza_catqP9LRfJOMybzoorpxSBxSP=J1o9WZnFp1USg@mail.gmail.com>
References: <CAP=2L=FRza_catqP9LRfJOMybzoorpxSBxSP=J1o9WZnFp1USg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>
Paolo Losi <paolo.losi@gmail.com> writes:
> I've noticed that order by / limit are not distributed to union subqueries
> by the planner:
> Example:
> q1: (select * from t1) union all (select * from t2) order by x limit 10;
> q2: (select * from t1 order by x limit 10) union all (select * from t2
> order by x limit 10)
> order by x limit 10;
> both queries should be equivalent, but the planner provides hugely different
> plans. I was expecting that the planner could rewrite the first to the
> second.
> Am I overlooking something? If this is the case, can anyone explain why this
> optimization is not performed?
There would be cases where that would be a win, and there would be cases
where it wouldn't be, so I'd not be in favor of making the transformation
blindly. Unfortunately, given the current state of the planner that's
all we could do really, because the subqueries are planned at arm's
length and then we just mechanically combine them. Doing it "right" would
entail fully planning each subquery twice, which would be very expensive.
I have a longstanding desire to rewrite the upper levels of the planner to
use path generation and comparison, which should make it more practical
for the planner to compare alternative implementations of UNION and other
top-level constructs. But I've been saying I would do that for several
years now, so don't hold your breath :-(
regards, tom lane
--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
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 using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: tgl@sss.pgh.pa.us, paolo.losi@gmail.com
Subject: Re: pushing order by + limit to union subqueries
In-Reply-To: <28519.1425140641@sss.pgh.pa.us>
* 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