gporca icon indicating copy to clipboard operation
gporca copied to clipboard

Orca generate bad plan that has duplicate Stream Aggregate nodes

Open hsyuan opened this issue 6 years ago • 0 comments

The query is extracted from issue #354.

I don't see the necessity of having 2 aggregates in 1 slice.

create table sale(sales_id int, product_id int, quantity int);
create table product (product_id int primary key, description text);   => add PK constraint
insert into sale select i, i, i  from generate_series(1,1000)i;
insert into product select i, 'abc'|| i from generate_series(1,1000)i;

set optimizer_enumerate_plans=on;
set optimizer_plan_id=2;

EXPLAIN SELECT sum(s.product_id) FROM sale s, product p 
WHERE p.product_id = s.product_id GROUP BY s.product_id;
                                                                   QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------
 Hash Join  (cost=0.00..863.00 rows=334 width=8)
   Hash Cond: product.product_id = sale.product_id
   ->  Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..431.02 rows=1000 width=4)
         ->  Table Scan on product  (cost=0.00..431.01 rows=334 width=4)
   ->  Hash  (cost=431.40..431.40 rows=334 width=12)
         ->  Gather Motion 3:1  (slice3; segments: 3)  (cost=0.00..431.40 rows=1000 width=12)
               ->  Result  (cost=0.00..431.36 rows=334 width=12)
                     ->  GroupAggregate  (cost=0.00..431.36 rows=334 width=12)
                           Group Key: sale.product_id
                           ->  Sort  (cost=0.00..431.35 rows=334 width=12)
                                 Sort Key: sale.product_id
                                 ->  Result  (cost=0.00..431.16 rows=334 width=12)
                                       ->  Result  (cost=0.00..431.15 rows=1000 width=12)
                                             ->  GroupAggregate  (cost=0.00..431.15 rows=1000 width=12)
                                                   Group Key: sale.product_id
                                                   ->  Sort  (cost=0.00..431.15 rows=1000 width=4)
                                                         Sort Key: sale.product_id
                                                         ->  Broadcast Motion 3:3  (slice2; segments: 3)  (cost=0.00..431.08 rows=1000 width=4)
                                                               ->  Table Scan on sale  (cost=0.00..431.01 rows=334 width=4)
 Optimizer: PQO version 2.55.13
(20 rows)

hsyuan avatar Jun 06 '18 20:06 hsyuan