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 '%unusual%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=851688.19..851688.69 rows=200 width=8) (actual time=40425.925..40425.927 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=187158 read=103924 written=1348 -> HashAggregate (cost=851678.55..851680.55 rows=200 width=8) (actual time=40425.803..40425.817 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=187155 read=103924 written=1348 -> HashAggregate (cost=814178.55..829178.55 rows=1500000 width=8) (actual time=39451.248..40098.529 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=187155 read=103924 written=1348 -> Hash Right Join (cost=69028.00..739184.78 rows=14998753 width=8) (actual time=1232.468..22723.037 rows=15352726 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=187155 read=103924 written=1348 -> Seq Scan on orders (cost=0.00..445175.49 rows=14998753 width=8) (actual time=0.028..8822.367 rows=14852705 loops=1) Filter: ((o_comment)::text !~~ '%unusual%packages%'::text) Rows Removed by Filter: 162295 Buffers: shared hit=176659 read=79142 written=1339 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1219.008..1219.008 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=10496 read=24782 written=9 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.018..689.793 rows=1500000 loops=1) Buffers: shared hit=10496 read=24782 written=9 Planning time: 3.499 ms Execution time: 40458.428 ms (24 rows) COMMIT; COMMIT