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 ('13', '14', '12', '11', '27', '32', '22') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('13', '14', '12', '11', '27', '32', '22') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694648.59..694648.60 rows=3 width=20) (actual time=10616.666..10616.667 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=215855 read=111230 dirtied=254 written=86 InitPlan 1 (returns $0) -> Aggregate (cost=52881.04..52881.05 rows=1 width=4) (actual time=478.673..478.673 rows=1 loops=1) Buffers: shared hit=33564 read=2776 written=5 -> Bitmap Heap Scan on customer customer_1 (cost=6641.78..51927.89 rows=381261 width=4) (actual time=93.598..405.267 rows=381049 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{13,14,12,11,27,32,22}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=33564 read=2776 written=5 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6546.47 rows=381261 width=0) (actual time=85.126..85.126 rows=381049 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{13,14,12,11,27,32,22}'::text[])) Buffers: shared hit=10 read=1052 -> HashAggregate (cost=641767.47..641767.51 rows=3 width=20) (actual time=10616.603..10616.606 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=215852 read=111230 dirtied=254 written=86 -> Hash Anti Join (cost=597810.32..641321.70 rows=59436 width=20) (actual time=10225.158..10595.014 rows=63227 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=215852 read=111230 dirtied=254 written=86 -> Bitmap Heap Scan on customer (cost=2907.99..41855.80 rows=139802 width=24) (actual time=539.689..788.443 rows=190186 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{13,14,12,11,27,32,22}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35153 Buffers: shared hit=60032 read=12006 written=5 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2873.04 rows=139802 width=0) (actual time=531.090..531.090 rows=190186 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{13,14,12,11,27,32,22}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=33574 read=3311 written=5 -> Hash (cost=406090.48..406090.48 rows=15104948 width=4) (actual time=9146.499..9146.499 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=155817 read=99224 dirtied=254 written=81 -> Seq Scan on orders (cost=0.00..406090.48 rows=15104948 width=4) (actual time=0.021..4455.867 rows=15015000 loops=1) Buffers: shared hit=155817 read=99224 dirtied=254 written=81 Planning time: 2.519 ms Execution time: 10640.085 ms (34 rows) COMMIT; COMMIT