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%requests%' 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=850074.61..850075.11 rows=200 width=8) (actual time=40294.927..40294.932 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=252644 read=37678 written=31 -> HashAggregate (cost=850064.97..850066.97 rows=200 width=8) (actual time=40294.832..40294.839 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=252641 read=37678 written=31 -> HashAggregate (cost=812564.97..827564.97 rows=1500000 width=8) (actual time=39387.328..39985.664 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=252641 read=37678 written=31 -> Hash Right Join (cost=69028.00..737643.94 rows=14984206 width=8) (actual time=1150.172..23918.353 rows=15353544 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=252641 read=37678 written=31 -> Seq Scan on orders (cost=0.00..443852.85 rows=14984206 width=8) (actual time=0.029..7537.843 rows=14853526 loops=1) Filter: ((o_comment)::text !~~ '%unusual%requests%'::text) Rows Removed by Filter: 161474 Buffers: shared hit=252639 read=2402 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1138.609..1138.609 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 written=31 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.010..718.864 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=31 Planning time: 3.133 ms Execution time: 40321.680 ms (24 rows) COMMIT; COMMIT