Re: Use LIMIT instead of Unique for DISTINCT when all distinct pathkeys are redundant

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: Richard Guo <guofenglinux(at)gmail(dot)com>
Cc: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Use LIMIT instead of Unique for DISTINCT when all distinct pathkeys are redundant
Date: 2022-10-12 20:50:30
Message-ID: CAApHDvrK-Ba5LtCuN9=B81S9bg8C9ov8J69DndraKGh-o94uFg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, 13 Oct 2022 at 01:13, Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
> For DISTINCT ON, if all the distinct pathkeys are redundant but there
> are available sort pathkeys, then for adequately-presorted paths I think
> we can also apply this optimization, using a Limit 1 rather than Unique.
>
> regression=# explain (analyze, costs off, timing off) select distinct on (four) * from tenk1 where four = 0 order by four, hundred desc;
> QUERY PLAN
> --------------------------------------------------------------------------------
> Limit (actual rows=1 loops=1)
> -> Index Scan Backward using tenk1_hundred on tenk1 (actual rows=1 loops=1)
> Filter: (four = 0)
> Rows Removed by Filter: 300

I don't think we can optimise this case, at least not the same way I'm
doing it in the patch I attached.

The problem is that I'm only added the LimitPath to the
cheapest_total_path. I think to make your case work we'd need to add
the LimitPath only in cases where the distinct_pathkeys are empty but
the sort_pathkeys are not and hasDistinctOn is true and the path has
pathkeys_contained_in(root->sort_pathkeys, path->pathkeys). I think
that's doable, but it's become quite a bit more complex than the patch
I proposed. Maybe it's worth a 2nd effort for that part?

David

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2022-10-12 20:59:37 Re: allowing for control over SET ROLE
Previous Message David Rowley 2022-10-12 20:41:11 Re: Use LIMIT instead of Unique for DISTINCT when all distinct pathkeys are redundant