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%deposits%' 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=851222.99..851223.49 rows=200 width=8) (actual time=35462.235..35462.238 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=231222 read=60113 written=245 -> HashAggregate (cost=851213.35..851215.35 rows=200 width=8) (actual time=35462.138..35462.146 rows=45 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=231219 read=60113 written=245 -> HashAggregate (cost=813713.35..828713.35 rows=1500000 width=8) (actual time=34499.622..35142.016 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=231219 read=60113 written=245 -> Hash Right Join (cost=69028.00..738945.96 rows=14953478 width=8) (actual time=1282.381..21558.416 rows=15354687 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=231219 read=60113 written=245 -> Seq Scan on orders (cost=0.00..445615.79 rows=14953478 width=8) (actual time=0.026..8882.162 rows=14854669 loops=1) Filter: ((o_comment)::text !~~ '%express%deposits%'::text) Rows Removed by Filter: 160331 Buffers: shared hit=225419 read=30635 written=245 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1266.184..1266.184 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=5800 read=29478 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.016..748.525 rows=1500000 loops=1) Buffers: shared hit=5800 read=29478 Planning time: 3.018 ms Execution time: 35488.058 ms (24 rows) COMMIT; COMMIT