BEGIN; BEGIN EXPLAIN (ANALYZE, BUFFERS) 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 '%special%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=849479.89..849480.39 rows=200 width=8) (actual time=29502.124..29502.126 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=259521 read=30801 written=18 -> HashAggregate (cost=849470.25..849472.25 rows=200 width=8) (actual time=29502.024..29502.033 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=259518 read=30801 written=18 -> HashAggregate (cost=811970.25..826970.25 rows=1500000 width=8) (actual time=28364.363..29184.477 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=259518 read=30801 written=18 -> Hash Right Join (cost=69028.00..737197.90 rows=14954470 width=8) (actual time=918.322..18627.390 rows=15353306 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=259518 read=30801 written=18 -> Seq Scan on orders (cost=0.00..443852.85 rows=14954470 width=8) (actual time=0.028..8313.626 rows=14853289 loops=1) Filter: ((o_comment)::text !~~ '%special%packages%'::text) Rows Removed by Filter: 161711 Buffers: shared hit=224240 read=30801 written=18 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=910.647..910.647 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.018..506.691 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 2.856 ms Execution time: 29528.207 ms (24 rows) COMMIT; COMMIT