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=917502.17..917502.67 rows=200 width=8) (actual time=32515.962..32515.965 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=279364 read=9439 -> HashAggregate (cost=917492.53..917494.53 rows=200 width=8) (actual time=32515.825..32515.829 rows=45 loops=1) Buffers: shared hit=279361 read=9439 -> HashAggregate (cost=879992.53..894992.53 rows=1500000 width=8) (actual time=31769.504..32213.819 rows=1500000 loops=1) Buffers: shared hit=279361 read=9439 -> Hash Right Join (cost=69028.00..806041.48 rows=14790209 width=8) (actual time=831.167..23944.083 rows=15354693 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=279361 read=9439 -> Seq Scan on orders (cost=0.00..441209.30 rows=14790209 width=8) (actual time=0.039..8727.946 rows=14854672 loops=1) Filter: ((o_comment)::text !~~ '%express%deposits%'::text) Rows Removed by Filter: 160328 Buffers: shared hit=251577 read=1945 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=828.954..828.954 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=27784 read=7494 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.025..491.377 rows=1500000 loops=1) Buffers: shared hit=27784 read=7494 Total runtime: 32539.645 ms (21 rows) COMMIT; COMMIT