Re: Overhead of union versus union all - Mailing list pgsql-general

From Bruce Momjian
Subject Re: Overhead of union versus union all
Date
Msg-id 200907101346.n6ADkNP01928@momjian.us
Whole thread Raw
In response to Re: Overhead of union versus union all  (Simon Riggs <simon@2ndQuadrant.com>)
Responses Re: Overhead of union versus union all
List pgsql-general
Simon Riggs wrote:
> or a query like this
>
>  Select '1', ...
>  ...
>  union
>  Select status, ...
>  ...
>  where status != '1';
>  ;
>
> then it is clear that we could automatically prove that the the distinct
> step is redundant and so we could either hash or sort. This is the same
> as replacing the UNION with UNION ALL.

In the last example, how do you know that status != '1' produces unique
output?  I assumed UNION gave distinct for the entire output, not just
remove duplicates from the two UNION branches;  that's how Postgres
behaves now:

    test=> SELECT 1 UNION (SELECT 2 UNION ALL SELECT 2);
     ?column?
    ----------
            1
            2
    (2 rows)

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +

pgsql-general by date:

Previous
From: Simon Riggs
Date:
Subject: Re: Overhead of union versus union all
Next
From: Simon Riggs
Date:
Subject: Re: Overhead of union versus union all