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%deposits%' 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=925874.18..925874.68 rows=200 width=8) (actual time=64961.997..64962.001 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=290728 read=101 -> HashAggregate (cost=925864.54..925866.54 rows=200 width=8) (actual time=64961.896..64961.908 rows=45 loops=1) Buffers: shared hit=290725 read=101 -> HashAggregate (cost=888364.54..903364.54 rows=1500000 width=8) (actual time=63708.663..64621.543 rows=1500000 loops=1) Buffers: shared hit=290725 read=101 -> Hash Right Join (cost=69028.00..813444.27 rows=14984054 width=8) (actual time=1721.862..46491.684 rows=15352907 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=290725 read=101 -> Seq Scan on orders (cost=0.00..444735.19 rows=14984054 width=8) (actual time=0.051..11144.857 rows=14852888 loops=1) Filter: ((o_comment)::text !~~ '%unusual%deposits%'::text) Rows Removed by Filter: 162112 Buffers: shared hit=255520 read=28 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1719.622..1719.622 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35205 read=73 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.021..959.854 rows=1500000 loops=1) Buffers: shared hit=35205 read=73 Total runtime: 64991.822 ms (21 rows) COMMIT; COMMIT