Discussion about this post

User's avatar
Alexander's avatar

Hello Andrey.

I would like to add Pull-up subqueries - indeed, this is a very necessary thing, but in my ORM I also encounter requests of the following type:

EXPLAIN (ANALYZE, TIMING OFF, COSTS ON)

SELECT * FROM tenk1 A

WHERE exists (SELECT TRUE

from (SELECT TRUE AS f1) f1_

INNER JOIN tenk2 B

ON B.hundred = A.hundred AND B.unique1 = A.odd

INNER JOIN onek C

ON B.unique1 = C.odd

WHERE B.two = 0 and C.two = 0);

In this case we get a subplan. Although if the query is slightly modified, it will be a SEMI JOIN:

EXPLAIN (ANALYZE, TIMING OFF, COSTS ON)

SELECT * FROM tenk1 A

WHERE exists (SELECT TRUE

FROM tenk2 B

INNER JOIN onek C

ON B.unique1 = C.odd

WHERE B.two = 0 and C.two = 0 and B.hundred = A.hundred AND B.unique1 = A.odd);

The essence of the queries is the same, but the optimizer selects different plans and, as in your case, the execution time with a subplan will be significantly longer. Why does the optimizer choose a subplan?

Expand full comment
Tomas's avatar

It's not clear to me what transformations to pull-up correlated subqueries you have in mind. Perhaps the best way to get that happen is to post some proposal on the hackers mailing list, with a description of the benefits and how the planner might decide to apply the transformation. I'd expect that to be the challenging part - costing the alternative transformations.

That was mostly the problem with the GROUP BY reordering - the statistics we have are not sufficient to make reliable decisions, and it got the patch reverted. The incremental sort has somewhat similar challenges, FWIW.

Expand full comment
2 more comments...

No posts