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 '%pending%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=929115.21..929115.71 rows=200 width=8) (actual time=60450.335..60450.337 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=291672 read=169 -> HashAggregate (cost=929105.56..929107.56 rows=200 width=8) (actual time=60450.225..60450.235 rows=46 loops=1) Buffers: shared hit=291669 read=169 -> HashAggregate (cost=891605.56..906605.56 rows=1500000 width=8) (actual time=59361.217..60107.604 rows=1500000 loops=1) Buffers: shared hit=291669 read=169 -> Hash Right Join (cost=69028.00..816389.33 rows=15043247 width=8) (actual time=1394.009..42960.404 rows=15352973 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=291669 read=169 -> Seq Scan on orders (cost=0.00..446496.39 rows=15043247 width=8) (actual time=0.046..9591.533 rows=14852952 loops=1) Filter: ((o_comment)::text !~~ '%pending%deposits%'::text) Rows Removed by Filter: 162048 Buffers: shared hit=256391 read=169 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1391.733..1391.733 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.034..760.866 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 60479.762 ms (21 rows) COMMIT; COMMIT