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=923979.06..923979.56 rows=200 width=8) (actual time=60241.998..60242.001 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=290324 read=505 written=82 -> HashAggregate (cost=923969.41..923971.41 rows=200 width=8) (actual time=60241.878..60241.888 rows=45 loops=1) Buffers: shared hit=290321 read=505 written=82 -> HashAggregate (cost=886469.41..901469.41 rows=1500000 width=8) (actual time=59153.344..59941.293 rows=1500000 loops=1) Buffers: shared hit=290321 read=505 written=82 -> Hash Right Join (cost=69028.00..811928.17 rows=14908249 width=8) (actual time=1137.330..43481.829 rows=15352907 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=290321 read=505 written=82 -> Seq Scan on orders (cost=0.00..444735.19 rows=14908249 width=8) (actual time=0.030..9225.583 rows=14852888 loops=1) Filter: ((o_comment)::text !~~ '%unusual%deposits%'::text) Rows Removed by Filter: 162112 Buffers: shared hit=255049 read=499 written=82 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1135.330..1135.330 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35272 read=6 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.012..567.670 rows=1500000 loops=1) Buffers: shared hit=35272 read=6 Total runtime: 60282.982 ms (21 rows) COMMIT; COMMIT