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=926801.23..926801.73 rows=200 width=8) (actual time=65520.325..65520.329 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=290909 read=679 -> HashAggregate (cost=926791.59..926793.59 rows=200 width=8) (actual time=65520.193..65520.203 rows=46 loops=1) Buffers: shared hit=290906 read=679 -> HashAggregate (cost=889291.59..904291.59 rows=1500000 width=8) (actual time=64185.176..65140.456 rows=1500000 loops=1) Buffers: shared hit=290906 read=679 -> Hash Right Join (cost=69028.00..814450.09 rows=14968300 width=8) (actual time=1340.819..46702.747 rows=15352887 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=290906 read=679 -> Seq Scan on orders (cost=0.00..446056.09 rows=14968300 width=8) (actual time=0.047..11320.441 rows=14852868 loops=1) Filter: ((o_comment)::text !~~ '%unusual%deposits%'::text) Rows Removed by Filter: 162132 Buffers: shared hit=255628 read=679 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1338.741..1338.741 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.016..753.146 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 65549.273 ms (21 rows) COMMIT; COMMIT