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=847836.72..847837.22 rows=200 width=8) (actual time=36392.771..36392.774 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=220223 read=69845 written=889 -> HashAggregate (cost=847827.08..847829.08 rows=200 width=8) (actual time=36392.665..36392.676 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=220220 read=69845 written=889 -> HashAggregate (cost=810327.08..825327.08 rows=1500000 width=8) (actual time=35251.895..36038.414 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=220220 read=69845 written=889 -> Hash Right Join (cost=69028.00..735855.01 rows=14894414 width=8) (actual time=998.513..21313.096 rows=15353296 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=220220 read=69845 written=889 -> Seq Scan on orders (cost=0.00..443410.80 rows=14894414 width=8) (actual time=0.031..9283.240 rows=14853279 loops=1) Filter: ((o_comment)::text !~~ '%special%packages%'::text) Rows Removed by Filter: 161721 Buffers: shared hit=186304 read=68483 written=889 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=986.997..986.997 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=33916 read=1362 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.015..559.504 rows=1500000 loops=1) Buffers: shared hit=33916 read=1362 Planning time: 3.216 ms Execution time: 36412.727 ms (24 rows) COMMIT; COMMIT