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%accounts%' 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=852598.73..852599.23 rows=200 width=8) (actual time=35736.016..35736.020 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=223979 read=67103 written=190 -> HashAggregate (cost=852589.09..852591.09 rows=200 width=8) (actual time=35735.907..35735.916 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=223976 read=67103 written=190 -> HashAggregate (cost=815089.09..830089.09 rows=1500000 width=8) (actual time=34573.731..35415.168 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=223976 read=67103 written=190 -> Hash Right Join (cost=69028.00..739867.69 rows=15044280 width=8) (actual time=1087.092..21867.159 rows=15353109 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=223976 read=67103 written=190 -> Seq Scan on orders (cost=0.00..445175.49 rows=15044280 width=8) (actual time=0.031..7913.381 rows=14853093 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161907 Buffers: shared hit=188698 read=67103 written=190 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1069.912..1069.912 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..531.528 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 2.630 ms Execution time: 35769.104 ms (24 rows) COMMIT; COMMIT