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=924740.21..924740.71 rows=200 width=8) (actual time=91258.671..91258.674 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=283873 read=6956 -> HashAggregate (cost=924730.56..924732.56 rows=200 width=8) (actual time=91258.559..91258.569 rows=46 loops=1) Buffers: shared hit=283870 read=6956 -> HashAggregate (cost=887230.56..902230.56 rows=1500000 width=8) (actual time=90114.551..90901.359 rows=1500000 loops=1) Buffers: shared hit=283870 read=6956 -> Hash Right Join (cost=69028.00..812537.09 rows=14938695 width=8) (actual time=2858.268..67139.727 rows=15354096 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=283870 read=6956 -> Seq Scan on orders (cost=0.00..444735.19 rows=14938695 width=8) (actual time=0.046..19533.129 rows=14854075 loops=1) Filter: ((o_comment)::text !~~ '%express%packages%'::text) Rows Removed by Filter: 160925 Buffers: shared hit=254036 read=1512 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2856.062..2856.062 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=29834 read=5444 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.012..1815.654 rows=1500000 loops=1) Buffers: shared hit=29834 read=5444 Total runtime: 91305.012 ms (21 rows) COMMIT; COMMIT