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=918005.07..918005.57 rows=200 width=8) (actual time=66603.207..66603.211 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=223971 read=65338 written=105 -> HashAggregate (cost=917995.43..917997.43 rows=200 width=8) (actual time=66603.076..66603.089 rows=46 loops=1) Buffers: shared hit=223968 read=65338 written=105 -> HashAggregate (cost=880495.43..895495.43 rows=1500000 width=8) (actual time=65242.627..66234.926 rows=1500000 loops=1) Buffers: shared hit=223968 read=65338 written=105 -> Hash Right Join (cost=69028.00..806619.92 rows=14775101 width=8) (actual time=1317.733..46932.303 rows=15354087 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=223968 read=65338 written=105 -> Seq Scan on orders (cost=0.00..442089.90 rows=14775101 width=8) (actual time=0.035..10520.517 rows=14854067 loops=1) Filter: ((o_comment)::text !~~ '%express%packages%'::text) Rows Removed by Filter: 160933 Buffers: shared hit=220075 read=33953 written=105 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1315.316..1315.316 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=3893 read=31385 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.016..760.386 rows=1500000 loops=1) Buffers: shared hit=3893 read=31385 Total runtime: 66649.986 ms (21 rows) COMMIT; COMMIT