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 '%express%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=851222.99..851223.49 rows=200 width=8) (actual time=40788.152..40788.156 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=211929 read=79406 dirtied=127 written=1972 -> HashAggregate (cost=851213.35..851215.35 rows=200 width=8) (actual time=40788.044..40788.054 rows=45 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=211926 read=79406 dirtied=127 written=1972 -> HashAggregate (cost=813713.35..828713.35 rows=1500000 width=8) (actual time=39913.979..40467.351 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=211926 read=79406 dirtied=127 written=1972 -> Hash Right Join (cost=69028.00..738945.96 rows=14953478 width=8) (actual time=1237.545..25144.648 rows=15339856 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=211926 read=79406 dirtied=127 written=1972 -> Seq Scan on orders (cost=0.00..445615.79 rows=14953478 width=8) (actual time=0.025..9299.658 rows=14839838 loops=1) Filter: ((o_comment)::text !~~ '%express%deposits%'::text) Rows Removed by Filter: 160162 Buffers: shared hit=211924 read=44130 dirtied=127 written=1968 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1221.282..1221.282 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 written=4 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.011..774.007 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=4 Planning time: 3.270 ms Execution time: 40825.017 ms (24 rows) COMMIT; COMMIT