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 '%unusual%packages%' 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=847394.66..847395.16 rows=200 width=8) (actual time=25690.476..25690.479 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=223026 read=66789 written=54 -> HashAggregate (cost=847385.02..847387.02 rows=200 width=8) (actual time=25690.379..25690.388 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=223023 read=66789 written=54 -> HashAggregate (cost=809885.02..824885.02 rows=1500000 width=8) (actual time=24964.645..25401.410 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=223023 read=66789 written=54 -> Hash Right Join (cost=69028.00..735413.39 rows=14894326 width=8) (actual time=920.552..16494.811 rows=15337937 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=223023 read=66789 written=54 -> Seq Scan on orders (cost=0.00..442970.50 rows=14894326 width=8) (actual time=0.034..7348.540 rows=14837916 loops=1) Filter: ((o_comment)::text !~~ '%unusual%packages%'::text) Rows Removed by Filter: 162084 Buffers: shared hit=193923 read=60611 written=54 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=909.222..909.222 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=29100 read=6178 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.013..519.571 rows=1500000 loops=1) Buffers: shared hit=29100 read=6178 Planning time: 2.614 ms Execution time: 25741.361 ms (24 rows) COMMIT; COMMIT