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=849772.69..849773.19 rows=200 width=8) (actual time=54034.619..54034.624 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=282676 read=7646 written=12 -> HashAggregate (cost=849763.05..849765.05 rows=200 width=8) (actual time=54034.514..54034.526 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=282673 read=7646 written=12 -> HashAggregate (cost=812263.05..827263.05 rows=1500000 width=8) (actual time=52896.555..53668.159 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=282673 read=7646 written=12 -> Hash Right Join (cost=69028.00..737417.50 rows=14969110 width=8) (actual time=1016.526..30853.334 rows=15354690 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=282673 read=7646 written=12 -> Seq Scan on orders (cost=0.00..443852.85 rows=14969110 width=8) (actual time=0.030..9825.222 rows=14854671 loops=1) Filter: ((o_comment)::text !~~ '%express%deposits%'::text) Rows Removed by Filter: 160329 Buffers: shared hit=247495 read=7546 written=12 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=998.850..998.850 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35178 read=100 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.020..543.922 rows=1500000 loops=1) Buffers: shared hit=35178 read=100 Planning time: 3.451 ms Execution time: 54077.489 ms (24 rows) COMMIT; COMMIT