gporca icon indicating copy to clipboard operation
gporca copied to clipboard

Orca failed to decorrelate subquery

Open hsyuan opened this issue 7 years ago • 1 comments

repro:

create table foo(a int, b int);
create table bar(a int, b int);
insert into foo select i,i from generate_series(1,1000) i;
insert into bar select i,i from generate_series(1,1000) i;

explain select * from foo where foo.b in (select foo.a+bar.b from bar );
                                                  QUERY PLAN
--------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..1370761.76 rows=2002 width=8)
   ->  Table Scan on foo  (cost=0.00..1370761.70 rows=668 width=8)
         Filter: (SubPlan 1)
         SubPlan 1
           ->  Result  (cost=0.00..431.09 rows=1000 width=4)
                 ->  Materialize  (cost=0.00..431.09 rows=1000 width=4)
                       ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.08 rows=1000 width=4)
                             ->  Table Scan on bar  (cost=0.00..431.01 rows=334 width=4)
 Settings:  optimizer=on
 Optimizer status: PQO version 2.51.4
(10 rows)

But planner successfully decorrelates it.

explain select * from foo where foo.b in (select foo.a+bar.b from bar );
                                             QUERY PLAN
-----------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=56.00..150229.02 rows=2002 width=8)
   ->  Nested Loop Semi Join  (cost=56.00..150229.02 rows=668 width=8)
         Join Filter: (foo.a + bar.b) = foo.b
         ->  Seq Scan on foo  (cost=0.00..23.02 rows=668 width=8)
         ->  Materialize  (cost=56.00..86.00 rows=1000 width=4)
               ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..53.00 rows=1000 width=4)
                     ->  Seq Scan on bar  (cost=0.00..13.00 rows=334 width=4)
 Settings:  optimizer=off
 Optimizer status: legacy query optimizer
(9 rows)

hsyuan avatar Dec 17 '17 22:12 hsyuan

execution time of orca plan doubles planner plan for this data set.

hsyuan avatar Dec 17 '17 22:12 hsyuan