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%accounts%' 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=852112.55..852113.05 rows=200 width=8) (actual time=30556.397..30556.399 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=223561 read=67774 written=548 -> HashAggregate (cost=852102.91..852104.91 rows=200 width=8) (actual time=30556.290..30556.300 rows=45 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=223558 read=67774 written=548 -> HashAggregate (cost=814602.91..829602.91 rows=1500000 width=8) (actual time=29438.484..30244.680 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=223558 read=67774 written=548 -> Hash Right Join (cost=69028.00..739613.13 rows=14997956 width=8) (actual time=1168.051..19352.526 rows=15354058 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=223558 read=67774 written=548 -> Seq Scan on orders (cost=0.00..445615.79 rows=14997956 width=8) (actual time=0.029..8553.700 rows=14854042 loops=1) Filter: ((o_comment)::text !~~ '%special%accounts%'::text) Rows Removed by Filter: 160958 Buffers: shared hit=223556 read=32498 written=52 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1152.877..1152.877 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 written=496 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.017..728.689 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=496 Planning time: 3.250 ms Execution time: 30585.445 ms (24 rows) COMMIT; COMMIT