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%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=921375.74..921376.24 rows=200 width=8) (actual time=78198.827..78198.832 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=261261 read=28048 -> HashAggregate (cost=921366.10..921368.10 rows=200 width=8) (actual time=78198.721..78198.731 rows=46 loops=1) Buffers: shared hit=261258 read=28048 -> HashAggregate (cost=883866.10..898866.10 rows=1500000 width=8) (actual time=77091.455..77842.256 rows=1500000 loops=1) Buffers: shared hit=261258 read=28048 -> Hash Right Join (cost=69028.00..809316.46 rows=14909928 width=8) (actual time=3192.346..57455.767 rows=15354087 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=261258 read=28048 -> Seq Scan on orders (cost=0.00..442089.90 rows=14909928 width=8) (actual time=0.031..16357.531 rows=14854067 loops=1) Filter: ((o_comment)::text !~~ '%express%packages%'::text) Rows Removed by Filter: 160933 Buffers: shared hit=251201 read=2827 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=3189.911..3189.911 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=10057 read=25221 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.023..2257.850 rows=1500000 loops=1) Buffers: shared hit=10057 read=25221 Total runtime: 78239.024 ms (21 rows) COMMIT; COMMIT