Re: Sorted union

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: <merlin(dot)moncure(at)rcsonline(dot)com>, <slamb(at)slamb(dot)org>
Cc: <pgsql-performance(at)postgresql(dot)org>, <dustin(at)spy(dot)net>
Subject: Re: Sorted union
Date: 2005-11-03 18:32:06
Message-ID: 436A03660200002500000452@gwmta.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

The ANSI/ISO specs are not at all ambiguous on this. An
ORDER BY is not allowed for the SELECT statements within
a UNION. It must come at the end and applied to the resulting
UNION.

Similarly, the column names in the result come from the first
query in the UNION. Column names in the query on the right
side of a UNION are immaterial.

Unless we have reason to believe that PostgreSQL is
non-compliant on this point, I don't think it is a good idea to
slow the query down with the subquery.

-Kevin

>>> "Merlin Moncure" <merlin(dot)moncure(at)rcsonline(dot)com> >>>

> Merlin Moncure wrote:
> > hmm, try pushing the union into a subquery...this is better style
> > because it's kind of ambiguous if the ordering will apply
before/after
> > the union.
>
> Seems to be a little slower. There's a new "subquery scan" step.

I figured. However it's more correct, I'm not sure if the original
query is necessarily guaranteed to give the right answer (in terms of
ordering). It might though.

Browse pgsql-performance by date

  From Date Subject
Next Message Merlin Moncure 2005-11-03 18:37:52 Re: Sorted union
Previous Message Merlin Moncure 2005-11-03 18:21:07 Re: Sorted union