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 '%pending%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=917196.99..917197.49 rows=200 width=8) (actual time=82200.482..82200.483 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=240599 read=48457 written=133 -> HashAggregate (cost=917187.35..917189.35 rows=200 width=8) (actual time=82200.364..82200.376 rows=46 loops=1) Buffers: shared hit=240596 read=48457 written=133 -> HashAggregate (cost=879687.35..894687.35 rows=1500000 width=8) (actual time=80994.148..81844.755 rows=1500000 loops=1) Buffers: shared hit=240596 read=48457 written=133 -> Hash Right Join (cost=69028.00..805885.40 rows=14760390 width=8) (actual time=2329.999..57575.589 rows=15353001 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=240596 read=48457 written=133 -> Seq Scan on orders (cost=0.00..441649.60 rows=14760390 width=8) (actual time=0.039..13408.760 rows=14852980 loops=1) Filter: ((o_comment)::text !~~ '%pending%deposits%'::text) Rows Removed by Filter: 162020 Buffers: shared hit=240593 read=13182 written=133 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2328.256..2328.256 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.009..1456.871 rows=1500000 loops=1) Buffers: shared hit=3 read=35275 Total runtime: 82257.895 ms (21 rows) COMMIT; COMMIT