Re: Why not do distinct before SetOp

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: ma lz <ma100(at)hotmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Why not do distinct before SetOp
Date: 2024-11-04 15:18:04
Message-ID: 2631313.1730733484@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

David Rowley <dgrowleyml(at)gmail(dot)com> writes:
> On Mon, 4 Nov 2024 at 22:52, ma lz <ma100(at)hotmail(dot)com> wrote:
>> select distinct a from t1 intersect select distinct a from t1; — this is faster than origin sql

> No, the planner does not attempt that optimisation. INTERSECT really
> isn't very well optimised.

It's not really obvious to me why adding DISTINCT would make it
faster. Seems like having two layers of plan nodes checking for
duplicate rows ought to be a loss. Maybe we need to do some
micro-optimization in or near LookupTupleHashEntry.

A different idea that occurred to me while looking at this is:
why have we got all this machinery to add and check a flag
column, rather than arranging things so that the two input
relations are "outer" and "inner" children of the SetOp?
It's possible some of the performance difference reported here
is due to having to pass more tuples through the SubqueryScan
node (with its projection to add the flag) and Append node,
but we could remove those steps entirely.

> If we did want to improve this area, I think the first thing we'd want
> to do is use standard join types rather than HashSetOp Intersect to
> implement INTERSECT (without ALL). To do that efficiently, we'd need
> to do a bit more work on the standard join types to have them
> efficiently support IS NOT DISTINCT FROM clauses as the join keys.

Maybe. It'd be a big project, but we do get complaints every so
often about IS NOT DISTINCT FROM predicates not being efficient,
so the benefits would be wider than just INTERSECT.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jim Rosenberg 2024-11-04 20:02:03 Column name beginning with underscore ("_")?
Previous Message Costa Alexoglou 2024-11-04 13:35:23 Re: Used memory calculation in containers - docker stats and file cache