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%requests%' 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=919819.92..919820.42 rows=200 width=8) (actual time=65667.963..65667.965 rows=44 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=225218 read=63838 written=15 -> HashAggregate (cost=919810.27..919812.27 rows=200 width=8) (actual time=65667.856..65667.871 rows=44 loops=1) Buffers: shared hit=225215 read=63838 written=15 -> HashAggregate (cost=882310.27..897310.27 rows=1500000 width=8) (actual time=64478.000..65356.935 rows=1500000 loops=1) Buffers: shared hit=225215 read=63838 written=15 -> Hash Right Join (cost=69028.00..807983.74 rows=14865307 width=8) (actual time=1437.094..46683.117 rows=15353353 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=225215 read=63838 written=15 -> Seq Scan on orders (cost=0.00..441649.60 rows=14865307 width=8) (actual time=0.045..10456.226 rows=14853332 loops=1) Filter: ((o_comment)::text !~~ '%express%requests%'::text) Rows Removed by Filter: 161668 Buffers: shared hit=189937 read=63838 written=15 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1435.089..1435.089 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.013..684.355 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 65713.008 ms (21 rows) COMMIT; COMMIT