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%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=852890.57..852891.07 rows=200 width=8) (actual time=31513.151..31513.153 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=202343 read=88739 written=555 -> HashAggregate (cost=852880.93..852882.93 rows=200 width=8) (actual time=31513.055..31513.064 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=202340 read=88739 written=555 -> HashAggregate (cost=815380.93..830380.93 rows=1500000 width=8) (actual time=30568.010..31156.494 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=202340 read=88739 written=555 -> Hash Right Join (cost=69028.00..740086.57 rows=15058872 width=8) (actual time=1047.756..19491.725 rows=15353775 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=202340 read=88739 written=555 -> Seq Scan on orders (cost=0.00..445175.49 rows=15058872 width=8) (actual time=0.028..8420.949 rows=14853753 loops=1) Filter: ((o_comment)::text !~~ '%unusual%accounts%'::text) Rows Removed by Filter: 161247 Buffers: shared hit=179052 read=76749 written=549 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1034.691..1034.691 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=23288 read=11990 written=6 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.012..585.976 rows=1500000 loops=1) Buffers: shared hit=23288 read=11990 written=6 Planning time: 3.322 ms Execution time: 31544.358 ms (24 rows) COMMIT; COMMIT