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', '28', '29', '16', '11', '26', '17') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('13', '28', '29', '16', '11', '26', '17') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694136.97..694136.98 rows=3 width=20) (actual time=12995.446..12995.448 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=204191 read=122659 InitPlan 1 (returns $0) -> Aggregate (cost=52937.31..52937.32 rows=1 width=4) (actual time=525.906..525.906 rows=1 loops=1) Buffers: shared hit=35288 read=1054 -> Bitmap Heap Scan on customer customer_1 (cost=6653.72..51980.30 rows=382803 width=4) (actual time=95.594..444.909 rows=381783 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{13,28,29,16,11,26,17}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=35288 read=1054 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6558.02 rows=382803 width=0) (actual time=86.633..86.633 rows=381783 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{13,28,29,16,11,26,17}'::text[])) Buffers: shared hit=10 read=1054 -> HashAggregate (cost=641199.58..641199.63 rows=3 width=20) (actual time=12995.390..12995.393 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=204188 read=122659 -> Hash Anti Join (cost=597221.21..640753.03 rows=59540 width=20) (actual time=12269.883..12970.157 rows=63402 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=204188 read=122659 -> Bitmap Heap Scan on customer (cost=2911.37..41867.86 rows=140133 width=24) (actual time=589.523..1151.609 rows=190840 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{13,28,29,16,11,26,17}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35170 Buffers: shared hit=35299 read=36758 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2876.33 rows=140133 width=0) (actual time=580.644..580.644 rows=190840 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{13,28,29,16,11,26,17}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35297 read=1590 -> Hash (cost=405686.04..405686.04 rows=15089904 width=4) (actual time=10013.996..10013.996 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=168886 read=85901 -> Seq Scan on orders (cost=0.00..405686.04 rows=15089904 width=4) (actual time=0.196..5104.845 rows=15015000 loops=1) Buffers: shared hit=168886 read=85901 Planning time: 2.611 ms Execution time: 13012.876 ms (34 rows) COMMIT; COMMIT