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 ('15', '30', '31', '28', '34', '22', '23') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('15', '30', '31', '28', '34', '22', '23') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=691175.77..691175.78 rows=3 width=20) (actual time=10157.948..10157.948 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=323998 read=1593 InitPlan 1 (returns $0) -> Aggregate (cost=52911.96..52911.97 rows=1 width=4) (actual time=468.588..468.589 rows=1 loops=1) Buffers: shared hit=35287 read=1056 -> Bitmap Heap Scan on customer customer_1 (cost=6648.35..51956.68 rows=382108 width=4) (actual time=90.875..392.872 rows=381774 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{15,30,31,28,34,22,23}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=35287 read=1056 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6552.82 rows=382108 width=0) (actual time=82.614..82.614 rows=381774 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{15,30,31,28,34,22,23}'::text[])) Buffers: shared hit=9 read=1056 -> HashAggregate (cost=638263.74..638263.78 rows=3 width=20) (actual time=10157.847..10157.848 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=323995 read=1593 -> Hash Anti Join (cost=594271.01..637821.64 rows=58946 width=20) (actual time=9845.136..10140.386 rows=63518 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=323995 read=1593 -> Bitmap Heap Scan on customer (cost=2911.87..41869.59 rows=140180 width=24) (actual time=520.665..675.871 rows=191024 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{15,30,31,28,34,22,23}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35173 Buffers: shared hit=70470 read=1593 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2876.82 rows=140180 width=0) (actual time=513.434..513.434 rows=191024 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{15,30,31,28,34,22,23}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35297 read=1593 -> Hash (cost=403671.84..403671.84 rows=15014984 width=4) (actual time=9205.311..9205.311 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=253522 -> Seq Scan on orders (cost=0.00..403671.84 rows=15014984 width=4) (actual time=0.024..4209.970 rows=15015000 loops=1) Buffers: shared hit=253522 Planning time: 3.908 ms Execution time: 10164.204 ms (34 rows) COMMIT; COMMIT