Re: OR vs UNION vs UNION ALL

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Etienne Champetier <champetier(dot)etienne(at)gmail(dot)com>
Cc: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: OR vs UNION vs UNION ALL
Date: 2015-09-23 18:20:35
Message-ID: CAHyXU0zstNjDcaW-ZYOV_dh9rsLpE-N05x37r_waS=jOjo5WYA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Sep 23, 2015 at 8:38 AM, Etienne Champetier
<champetier(dot)etienne(at)gmail(dot)com> wrote:
> Two questions:
> 1) Is it normal to have such a big difference between OR and UNION and
> should i always prefer UNION when possible?

yes, it's unfortunate but true. I consider automatic transformation
of these expressions to possible enhancements to the planner.
Although the fact it hasn't already been done hints at high difficulty
or complicated trade-offs.

Note, these queries are not the same. OR/UNION ALL should give the
same result regardless of input but UNION has a deduplication step
that gives a different answer. Avoid pure UNION unless it's very
specifically what you want; it can act as an optimization fence in
more complex queries.

> 2) How can UNION ALL be slower than UNION, it's not doing the HashAggregate
> but is 0.2s slower?

likely measurement noise. take 10 samples and take a median.

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Janes 2015-09-23 22:31:05 Re: pgcrypto
Previous Message Francisco Olarte 2015-09-23 17:46:32 Re: Table using more disk space than expected