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=847721.54..847722.04 rows=200 width=8) (actual time=25100.265..25100.267 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=289309 -> HashAggregate (cost=847711.90..847713.90 rows=200 width=8) (actual time=25100.161..25100.169 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=289306 -> HashAggregate (cost=810211.90..825211.90 rows=1500000 width=8) (actual time=24351.922..24795.415 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=289306 -> Hash Right Join (cost=69028.00..735438.40 rows=14954700 width=8) (actual time=763.016..15868.172 rows=15337935 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=289306 -> Seq Scan on orders (cost=0.00..442089.90 rows=14954700 width=8) (actual time=0.023..7044.607 rows=14837914 loops=1) Filter: ((o_comment)::text !~~ '%unusual%packages%'::text) Rows Removed by Filter: 162086 Buffers: shared hit=254028 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=752.969..752.969 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.010..389.091 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 2.385 ms Execution time: 25120.628 ms (24 rows) COMMIT; COMMIT