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=845628.44..845628.94 rows=200 width=8) (actual time=31135.155..31135.160 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=191979 read=97330 written=15 -> HashAggregate (cost=845618.80..845620.80 rows=200 width=8) (actual time=31135.018..31135.031 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=191976 read=97330 written=15 -> HashAggregate (cost=808118.80..823118.80 rows=1500000 width=8) (actual time=29922.123..30549.799 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=191976 read=97330 written=15 -> Hash Right Join (cost=69028.00..733868.57 rows=14850045 width=8) (actual time=1350.924..20097.720 rows=15353275 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=191976 read=97330 written=15 -> Seq Scan on orders (cost=0.00..442089.90 rows=14850045 width=8) (actual time=0.036..9165.450 rows=14853258 loops=1) Filter: ((o_comment)::text !~~ '%special%packages%'::text) Rows Removed by Filter: 161742 Buffers: shared hit=191974 read=62054 written=2 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1338.013..1338.013 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 written=13 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.021..862.236 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=13 Planning time: 5.007 ms Execution time: 31165.755 ms (24 rows) COMMIT; COMMIT