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=26161.529..26161.532 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=283965 read=5850 -> HashAggregate (cost=847385.02..847387.02 rows=200 width=8) (actual time=26161.412..26161.417 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=283962 read=5850 -> HashAggregate (cost=809885.02..824885.02 rows=1500000 width=8) (actual time=25423.597..25868.279 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=283962 read=5850 -> Hash Right Join (cost=69028.00..735413.39 rows=14894326 width=8) (actual time=835.329..17132.612 rows=15337937 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=283962 read=5850 -> Seq Scan on orders (cost=0.00..442970.50 rows=14894326 width=8) (actual time=0.041..7889.300 rows=14837916 loops=1) Filter: ((o_comment)::text !~~ '%unusual%packages%'::text) Rows Removed by Filter: 162084 Buffers: shared hit=248684 read=5850 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=820.005..820.005 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.023..429.172 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 2.744 ms Execution time: 26185.449 ms (24 rows) COMMIT; COMMIT