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 ('24', '30', '27', '34', '13', '19', '16') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('24', '30', '27', '34', '13', '19', '16') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=695932.61..695932.62 rows=3 width=20) (actual time=30055.567..30055.567 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=255440 read=72177 InitPlan 1 (returns $0) -> Aggregate (cost=52915.58..52915.59 rows=1 width=4) (actual time=3943.982..3943.982 rows=1 loops=1) Buffers: shared hit=264 read=36079 -> Bitmap Heap Scan on customer (cost=6648.23..51959.98 rows=382238 width=4) (actual time=283.574..3802.510 rows=381497 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,30,27,34,13,19,16}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=264 read=36079 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6552.67 rows=382238 width=0) (actual time=268.574..268.574 rows=381497 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{24,30,27,34,13,19,16}'::text[])) Buffers: shared hit=15 read=1050 -> HashAggregate (cost=643016.96..643017.00 rows=3 width=20) (actual time=30055.486..30055.490 rows=7 loops=1) Buffers: shared hit=255438 read=72177 -> Hash Anti Join (cost=598995.23..642569.95 rows=59601 width=20) (actual time=26455.041..29988.396 rows=63519 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=255438 read=72177 -> Bitmap Heap Scan on customer (cost=2910.29..41866.70 rows=140130 width=24) (actual time=4117.050..7208.474 rows=190540 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,30,27,34,13,19,16}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=315 read=71749 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2875.26 rows=140130 width=0) (actual time=4105.983..4105.983 rows=190540 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,30,27,34,13,19,16}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=275 read=36614 -> Hash (cost=406897.75..406897.75 rows=15134975 width=4) (actual time=22328.142..22328.142 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=255120 read=428 -> Seq Scan on orders (cost=0.00..406897.75 rows=15134975 width=4) (actual time=0.030..13870.812 rows=15015000 loops=1) Buffers: shared hit=255120 read=428 Total runtime: 30143.095 ms (30 rows) COMMIT; COMMIT