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=851384.17..851384.67 rows=200 width=8) (actual time=46645.875..46645.878 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=198859 read=92223 dirtied=253 written=605 -> HashAggregate (cost=851374.53..851376.53 rows=200 width=8) (actual time=46645.758..46645.767 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=198856 read=92223 dirtied=253 written=605 -> HashAggregate (cost=813874.53..828874.53 rows=1500000 width=8) (actual time=45380.880..46300.840 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=198856 read=92223 dirtied=253 written=605 -> Hash Right Join (cost=69028.00..738956.77 rows=14983552 width=8) (actual time=1266.019..26452.862 rows=15353109 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=198856 read=92223 dirtied=253 written=605 -> Seq Scan on orders (cost=0.00..445175.49 rows=14983552 width=8) (actual time=0.031..9173.010 rows=14853093 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161907 Buffers: shared hit=186914 read=68887 dirtied=253 written=561 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1253.701..1253.701 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=11942 read=23336 written=44 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.019..799.896 rows=1500000 loops=1) Buffers: shared hit=11942 read=23336 written=44 Planning time: 4.202 ms Execution time: 46680.298 ms (24 rows) COMMIT; COMMIT