BEGIN; BEGIN EXPLAIN select c_count, count(*) as custdist from ( select c_custkey, count(o_orderkey) from customer left outer join orders on c_custkey = o_custkey and o_comment not like '%pending%packages%' group by c_custkey ) as c_orders (c_custkey, c_count) group by c_count order by custdist desc, c_count desc; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=15422197.99..15422198.49 rows=200 width=8) Sort Key: (pg_catalog.count(*)) DESC, c_count DESC -> HashAggregate (cost=15422188.35..15422190.35 rows=200 width=8) Group Key: c_count -> Remote Subquery Scan on all (datanode_1_1,datanode_2_1,datanode_3_1,datanode_4_1,datanode_5_1,datanode_6_1,datanode_7_1,datanode_8_1) (cost=14724653.20..15347188.35 rows=15000000 width=8) -> HashAggregate (cost=14724653.20..15347188.35 rows=200 width=8) Group Key: count(orders.o_orderkey) -> GroupAggregate (cost=14724653.20..15197188.35 rows=15000000 width=8) Group Key: customer.c_custkey -> Sort (cost=14724653.20..14832164.91 rows=43004687 width=8) Sort Key: customer.c_custkey -> Hash Right Join (cost=690271.00..8684132.96 rows=43004687 width=8) Hash Cond: (orders.o_custkey = customer.c_custkey) -> Remote Subquery Scan on all (datanode_1_1,datanode_2_1,datanode_3_1,datanode_4_1,datanode_5_1,datanode_6_1,datanode_7_1,datanode_8_1) (cost=100.00..6338672.42 rows=148514263 width=8) Distribute results by H: o_custkey -> Seq Scan on orders (cost=0.00..4407887.00 rows=148514263 width=8) Filter: ((o_comment)::text !~~ '%pending%packages%'::text) -> Hash (cost=637871.00..637871.00 rows=15000000 width=4) -> Remote Subquery Scan on all (datanode_8_1) (cost=100.00..637871.00 rows=15000000 width=4) Distribute results by H: c_custkey -> Seq Scan on customer (cost=0.00..502771.00 rows=15000000 width=4) (21 rows) COMMIT; COMMIT