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=927495.81..927496.31 rows=200 width=8) (actual time=60705.968..60705.971 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=234573 read=56762 written=31 -> HashAggregate (cost=927486.16..927488.16 rows=200 width=8) (actual time=60705.840..60705.852 rows=46 loops=1) Buffers: shared hit=234570 read=56762 written=31 -> HashAggregate (cost=889986.16..904986.16 rows=1500000 width=8) (actual time=59474.672..60377.449 rows=1500000 loops=1) Buffers: shared hit=234570 read=56762 written=31 -> Hash Right Join (cost=69028.00..814917.69 rows=15013695 width=8) (actual time=1812.234..42798.461 rows=15337869 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=234570 read=56762 written=31 -> Seq Scan on orders (cost=0.00..445615.79 rows=15013695 width=8) (actual time=0.037..9404.846 rows=14837848 loops=1) Filter: ((o_comment)::text !~~ '%unusual%packages%'::text) Rows Removed by Filter: 162152 Buffers: shared hit=234567 read=21487 written=31 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1810.577..1810.577 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=3 read=35275 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.014..713.757 rows=1500000 loops=1) Buffers: shared hit=3 read=35275 Total runtime: 60730.550 ms (21 rows) COMMIT; COMMIT