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 '%express%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=846087.82..846088.32 rows=200 width=8) (actual time=46894.049..46894.053 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=174840 read=114216 written=1065 -> HashAggregate (cost=846078.18..846080.18 rows=200 width=8) (actual time=46893.929..46893.938 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=174837 read=114216 written=1065 -> HashAggregate (cost=808578.18..823578.18 rows=1500000 width=8) (actual time=45855.915..46571.596 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=174837 read=114216 written=1065 -> Hash Right Join (cost=69028.00..734103.04 rows=14895029 width=8) (actual time=1381.015..28860.197 rows=15354101 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=174837 read=114216 written=1065 -> Seq Scan on orders (cost=0.00..441649.60 rows=14895029 width=8) (actual time=0.031..9389.251 rows=14854081 loops=1) Filter: ((o_comment)::text !~~ '%express%packages%'::text) Rows Removed by Filter: 160919 Buffers: shared hit=174835 read=78940 written=1027 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1362.085..1362.085 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 written=38 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.020..772.036 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=38 Planning time: 3.615 ms Execution time: 46932.430 ms (24 rows) COMMIT; COMMIT