Re: Sorted union - Mailing list pgsql-performance

From Kevin Grittner
Subject Re: Sorted union
Date
Msg-id 436A03660200002500000452@gwmta.wicourts.gov
Whole thread Raw
In response to Sorted union  (Scott Lamb <slamb@slamb.org>)
List 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.moncure@rcsonline.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.


pgsql-performance by date:

Previous
From: "Merlin Moncure"
Date:
Subject: Re: Sorted union
Next
From: "Merlin Moncure"
Date:
Subject: Re: Sorted union