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=846968.24..846968.74 rows=200 width=8) (actual time=44768.930..44768.933 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=200384 read=89178 dirtied=120 written=532 -> HashAggregate (cost=846958.60..846960.60 rows=200 width=8) (actual time=44768.831..44768.845 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=200381 read=89178 dirtied=120 written=532 -> HashAggregate (cost=809458.60..824458.60 rows=1500000 width=8) (actual time=43576.604..44428.316 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=200381 read=89178 dirtied=120 written=532 -> Hash Right Join (cost=69028.00..734983.50 rows=14895020 width=8) (actual time=1269.529..26538.216 rows=15339254 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=200381 read=89178 dirtied=120 written=532 -> Seq Scan on orders (cost=0.00..442530.20 rows=14895020 width=8) (actual time=0.034..9640.554 rows=14839234 loops=1) Filter: ((o_comment)::text !~~ '%express%packages%'::text) Rows Removed by Filter: 160766 Buffers: shared hit=200379 read=53902 dirtied=120 written=532 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1259.080..1259.080 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.012..826.767 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Planning time: 5.306 ms Execution time: 45392.920 ms (24 rows) COMMIT; COMMIT