BEGIN; BEGIN EXPLAIN (ANALYZE, BUFFERS) select cntrycode, count(*) as numcust, sum(c_acctbal) as totacctbal from ( select substr(c_phone, 1, 2) as cntrycode, c_acctbal from customer where substr(c_phone, 1, 2) in ('33', '34', '29', '26', '27', '16', '17') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('33', '34', '29', '26', '27', '16', '17') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694615.11..694615.12 rows=3 width=20) (actual time=15460.696..15460.696 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=271510 read=55591 dirtied=42 written=9498 InitPlan 1 (returns $0) -> Aggregate (cost=52831.23..52831.24 rows=1 width=4) (actual time=943.715..943.715 rows=1 loops=1) Buffers: shared hit=14 read=36330 written=6062 -> Bitmap Heap Scan on customer (cost=6630.33..51881.41 rows=379927 width=4) (actual time=93.622..847.902 rows=382073 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{33,34,29,26,27,16,17}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=14 read=36330 written=6062 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6535.35 rows=379927 width=0) (actual time=84.440..84.440 rows=382073 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{33,34,29,26,27,16,17}'::text[])) Buffers: shared hit=12 read=1054 written=49 -> HashAggregate (cost=641783.80..641783.84 rows=3 width=20) (actual time=15460.630..15460.634 rows=7 loops=1) Buffers: shared hit=271508 read=55591 dirtied=42 written=9498 -> Hash Anti Join (cost=597805.23..641339.77 rows=59204 width=20) (actual time=14735.948..15411.840 rows=63612 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=271508 read=55591 dirtied=42 written=9498 -> Bitmap Heap Scan on customer (cost=2902.90..41840.46 rows=139412 width=24) (actual time=1054.853..1335.221 rows=191049 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{33,34,29,26,27,16,17}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35186 read=36869 written=6228 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2868.05 rows=139412 width=0) (actual time=1040.399..1040.399 rows=191049 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{33,34,29,26,27,16,17}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=23 read=36869 written=6228 -> Hash (cost=406090.48..406090.48 rows=15104948 width=4) (actual time=13669.027..13669.027 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=236319 read=18722 dirtied=42 written=3270 -> Seq Scan on orders (cost=0.00..406090.48 rows=15104948 width=4) (actual time=0.018..5574.054 rows=15015000 loops=1) Buffers: shared hit=236319 read=18722 dirtied=42 written=3270 Total runtime: 15588.298 ms (30 rows) COMMIT; COMMIT