Re: Wrong rows estimations with joins of CTEs slows queries by more than factor 500

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Jian Guo <gjian(at)vmware(dot)com>, Tomas Vondra <tomas(dot)vondra(at)enterprisedb(dot)com>, Hans Buschmann <buschmann(at)nidsa(dot)net>, "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Zhenghua Lyu <zlyu(at)vmware(dot)com>
Subject: Re: Wrong rows estimations with joins of CTEs slows queries by more than factor 500
Date: 2023-11-17 02:09:10
Message-ID: CAMbWs48Z_PvveOeankGA69=V=fqogusRKGVPBUGfYC0zDo1WPA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Nov 17, 2023 at 2:16 AM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> So you could argue that there's more to do here, but I'm hesitant
> to go further. Part of the point of MATERIALIZED is to be an
> optimization fence, so breaking down that fence is something to be
> wary of. Maybe we shouldn't even take this patch --- but on
> balance I think it's an OK compromise.

Agreed. I think the patch is still valuable on its own, although it
does not go down into MATERIALIZED case for further optimization. Maybe
we can take another query as regression test to prove its value, in
which the CTE is not inlined without MATERIALIZED, such as

explain (costs off)
with x as (select unique1, unique2 from tenk1 b)
select count(*) from tenk1 a
where unique1 in (select unique1 from x x1) and
unique1 in (select unique2 from x x2);
QUERY PLAN
------------------------------------------------------------------
Aggregate
CTE x
-> Seq Scan on tenk1 b
-> Hash Join
Hash Cond: (a.unique1 = x2.unique2)
-> Nested Loop
-> HashAggregate
Group Key: x1.unique1
-> CTE Scan on x x1
-> Index Only Scan using tenk1_unique1 on tenk1 a
Index Cond: (unique1 = x1.unique1)
-> Hash
-> HashAggregate
Group Key: x2.unique2
-> CTE Scan on x x2
(15 rows)

vs

explain (costs off)
with x as (select unique1, unique2 from tenk1 b)
select count(*) from tenk1 a
where unique1 in (select unique1 from x x1) and
unique1 in (select unique2 from x x2);
QUERY PLAN
------------------------------------------------------------------
Aggregate
CTE x
-> Seq Scan on tenk1 b
-> Hash Semi Join
Hash Cond: (a.unique1 = x2.unique2)
-> Hash Semi Join
Hash Cond: (a.unique1 = x1.unique1)
-> Index Only Scan using tenk1_unique1 on tenk1 a
-> Hash
-> CTE Scan on x x1
-> Hash
-> CTE Scan on x x2
(12 rows)

I believe the second plan is faster in reality too.

Thanks
Richard

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2023-11-17 02:26:58 Re: [HACKERS] Should logtape.c blocks be of type long?
Previous Message Zhijie Hou (Fujitsu) 2023-11-17 01:46:07 RE: Synchronizing slots from primary to standby