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 '%unusual%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=846983.08..846983.58 rows=200 width=8) (actual time=42302.717..42302.720 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=266573 read=22483 -> HashAggregate (cost=846973.44..846975.44 rows=200 width=8) (actual time=42302.581..42302.591 rows=45 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=266570 read=22483 -> HashAggregate (cost=809473.44..824473.44 rows=1500000 width=8) (actual time=41112.465..41957.790 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=266570 read=22483 -> Hash Right Join (cost=69028.00..734774.48 rows=14939792 width=8) (actual time=1529.063..26456.483 rows=15352905 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=266570 read=22483 -> Seq Scan on orders (cost=0.00..441649.60 rows=14939792 width=8) (actual time=0.036..9099.932 rows=14852886 loops=1) Filter: ((o_comment)::text !~~ '%unusual%deposits%'::text) Rows Removed by Filter: 162114 Buffers: shared hit=231292 read=22483 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1512.383..1512.383 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.023..814.647 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 3.915 ms Execution time: 42324.792 ms (24 rows) COMMIT; COMMIT