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=924187.62..924188.12 rows=200 width=8) (actual time=69677.722..69677.723 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=286990 read=3078 written=215 -> HashAggregate (cost=924177.97..924179.97 rows=200 width=8) (actual time=69677.596..69677.608 rows=46 loops=1) Buffers: shared hit=286987 read=3078 written=215 -> HashAggregate (cost=886677.97..901677.97 rows=1500000 width=8) (actual time=68522.264..69364.947 rows=1500000 loops=1) Buffers: shared hit=286987 read=3078 written=215 -> Hash Right Join (cost=69028.00..811830.14 rows=14969567 width=8) (actual time=1658.032..48644.354 rows=15354117 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=286987 read=3078 written=215 -> Seq Scan on orders (cost=0.00..443410.80 rows=14969567 width=8) (actual time=0.040..12421.830 rows=14854096 loops=1) Filter: ((o_comment)::text !~~ '%express%packages%'::text) Rows Removed by Filter: 160904 Buffers: shared hit=251709 read=3078 written=215 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1656.049..1656.049 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.017..952.084 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 69712.036 ms (21 rows) COMMIT; COMMIT