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=850316.99..850317.49 rows=200 width=8) (actual time=29302.579..29302.581 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=179398 read=111937 written=2397 -> HashAggregate (cost=850307.35..850309.35 rows=200 width=8) (actual time=29302.477..29302.482 rows=45 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=179395 read=111937 written=2397 -> HashAggregate (cost=812807.35..827807.35 rows=1500000 width=8) (actual time=28510.677..28980.844 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=179395 read=111937 written=2397 -> Hash Right Join (cost=69028.00..738266.46 rows=14908178 width=8) (actual time=1250.803..19465.535 rows=15354096 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=179395 read=111937 written=2397 -> Seq Scan on orders (cost=0.00..445615.79 rows=14908178 width=8) (actual time=0.024..9105.910 rows=14854075 loops=1) Filter: ((o_comment)::text !~~ '%express%packages%'::text) Rows Removed by Filter: 160925 Buffers: shared hit=179393 read=76661 written=2313 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1234.391..1234.391 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 written=84 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.016..790.534 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=84 Planning time: 2.971 ms Execution time: 29333.874 ms (24 rows) COMMIT; COMMIT