How to improve the estimated cardinality of inner join lateral
-
07-03-2021 - |
Domanda
I'm currently running a query that has the following structure:
select
v1.*,
v2.*,
...
from t1
inner join lateral f1(t1.id1, t1.id2) as v1 on true
inner join lateral f2(t1.id1, t1.id2) as v2 on true
...
where
t1.id1 = 123 and t1.id2 = 234;
This works fast enough, but I've noticed the following problem with explain analyze
.
For every inner join lateral, it seems to multiply the amount of estimated rows by 1000 even though the joins don't add new rows. This leads to the query plan having more rows that the jit_above_cost
which results in the JIT running and adding significant delay (450ms on a 40ms query).
I'd like to avoid disabling jit_above_cost
as I'm sure that it can be useful for optimizing queries, but I've tried searching for ways to improve the cardinality estimate, but I've had not luck.
Soluzione
If the joins can't add new rows, then why are f1 and f2 defined to be a set-returning function in the first place? If they were not set-returning functions, this multiplication would not happen.
Alternatively, declare the functions to be expected to return only one row.
alter function f1 rows 1;
etc.,.