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=844431.90..844432.40 rows=200 width=8) (actual time=33082.557..33082.560 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=239247 read=50062 written=2 -> HashAggregate (cost=844422.26..844424.26 rows=200 width=8) (actual time=33082.465..33082.472 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=239244 read=50062 written=2 -> HashAggregate (cost=806922.26..821922.26 rows=1500000 width=8) (actual time=32233.572..32767.501 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=239244 read=50062 written=2 -> Hash Right Join (cost=69028.00..732971.17 rows=14790218 width=8) (actual time=967.101..21873.083 rows=15354087 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=239244 read=50062 written=2 -> Seq Scan on orders (cost=0.00..442089.90 rows=14790218 width=8) (actual time=0.030..8423.824 rows=14854067 loops=1) Filter: ((o_comment)::text !~~ '%express%packages%'::text) Rows Removed by Filter: 160933 Buffers: shared hit=208942 read=45086 written=2 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=931.532..931.532 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=30302 read=4976 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.015..488.880 rows=1500000 loops=1) Buffers: shared hit=30302 read=4976 Planning time: 2.907 ms Execution time: 33119.409 ms (24 rows) COMMIT; COMMIT