pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Dimitrios Apostolou <jimis@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: Is "DISTINCT" making a diffence in:  SELECT [DISTINCT] ... EXCEPT
Date: Wed, 15 Nov 2023 19:15:18 +0100
Message-ID: <f4bd33ed0872a3b96d4cd27a2ac14b78eaa7e770.camel@cybertec.at> (raw)
In-Reply-To: <f13c9e32-8177-c5ab-ef41-5d8137a49da0@gmx.net>
References: <f13c9e32-8177-c5ab-ef41-5d8137a49da0@gmx.net>

On Wed, 2023-11-15 at 10:57 +0100, Dimitrios Apostolou wrote:
> SELECT [DISTINCT] ... EXCEPT ...
> 
> In this query I get the same results regardless of including DISTINCT or
> not. But I get different query plans, I get an extra HashAggregate node
> in the case of SELECT DISTINCT. Any idea why?

The DISTINCT is superfluous, because EXCEPT already removes duplicate rows.
However, the planner does not invest extra processing cycles to detect
that you wrote a superfluous DISTINCT, and it does not remove it.
As a consequence, you end up with a pointless extra execution plan node
that does not achieve anything except slowing down the query.

Remove the DISTINCT.

Yours,
Laurenz Albe





view thread (5+ messages)

Message-ID: <f4bd33ed0872a3b96d4cd27a2ac14b78eaa7e770.camel@cybertec.at>
Permalink:  ../f4bd33ed0872a3b96d4cd27a2ac14b78eaa7e770.camel@cybertec.at/
Also on:    postgresql.org/message-id/f4bd33ed0872a3b96d4cd27a2ac14b78eaa7e770.camel@cybertec.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: laurenz.albe@cybertec.at, jimis@gmx.net
  Subject: Re: Is "DISTINCT" making a diffence in:  SELECT [DISTINCT] ... EXCEPT
  In-Reply-To: <f4bd33ed0872a3b96d4cd27a2ac14b78eaa7e770.camel@cybertec.at>
  References: <f13c9e32-8177-c5ab-ef41-5d8137a49da0@gmx.net> <f4bd33ed0872a3b96d4cd27a2ac14b78eaa7e770.camel@cybertec.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox