Re: Overhead of union versus union all

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Tim Keitt <tkeitt(at)keittlab(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: Overhead of union versus union all
Date: 2009-07-09 20:14:43
Message-ID: 200907092014.n69KEh309113@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Alvaro Herrera wrote:
> Tim Keitt wrote:
> > I am combining query results that I know are disjoint. I'm wondering
> > how much overhead there is in calling union versus union all. (Just
> > curious really; I can't see a reason not to use union all.)
>
> UNION needs to uniquify the output, for which it plasters an additional
> sort step, whereas UNION ALL does not need to uniquify its output and
> thus it can avoid the sort step. Using UNION ALL is recommended
> wherever possible.

Yep, ideally UNION ALL would be the default behavior, but that standard
requires otherwise. Many people don't know that UNION has an extra
SORT/UNIQUE step.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message jacob 2009-07-09 20:18:08 Re: Help me please...
Previous Message Adam Rich 2009-07-09 20:11:55 Re: Overhead of union versus union all