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=929117.43..929117.93 rows=200 width=8) (actual time=87473.750..87473.754 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=252323 read=39518 dirtied=253 written=11 -> HashAggregate (cost=929107.79..929109.79 rows=200 width=8) (actual time=87473.644..87473.658 rows=46 loops=1) Buffers: shared hit=252320 read=39518 dirtied=253 written=11 -> HashAggregate (cost=891607.79..906607.79 rows=1500000 width=8) (actual time=86061.083..87106.753 rows=1500000 loops=1) Buffers: shared hit=252320 read=39518 dirtied=253 written=11 -> Hash Right Join (cost=69028.00..816391.11 rows=15043336 width=8) (actual time=4600.844..64493.259 rows=15352973 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=252320 read=39518 dirtied=253 written=11 -> Seq Scan on orders (cost=0.00..446496.39 rows=15043336 width=8) (actual time=0.055..20461.134 rows=14852952 loops=1) Filter: ((o_comment)::text !~~ '%pending%deposits%'::text) Rows Removed by Filter: 162048 Buffers: shared hit=252316 read=4244 dirtied=253 written=1 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=4598.858..4598.858 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=4 read=35274 written=10 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.012..3742.095 rows=1500000 loops=1) Buffers: shared hit=4 read=35274 written=10 Total runtime: 87501.033 ms (21 rows) COMMIT; COMMIT