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 ('29', '14', '13', '25', '23', '30', '21') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('29', '14', '13', '25', '23', '30', '21') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694872.48..694872.48 rows=3 width=20) (actual time=17338.555..17338.557 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=249852 read=77251 dirtied=501 written=159 InitPlan 1 (returns $0) -> Aggregate (cost=52973.95..52973.96 rows=1 width=4) (actual time=1477.464..1477.465 rows=1 loops=1) Buffers: shared hit=11 read=36331 written=141 -> Bitmap Heap Scan on customer (cost=6679.56..52016.00 rows=383179 width=4) (actual time=181.477..1349.164 rows=381569 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{29,14,13,25,23,30,21}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=11 read=36331 written=141 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6583.76 rows=383179 width=0) (actual time=171.201..171.201 rows=381569 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{29,14,13,25,23,30,21}'::text[])) Buffers: shared hit=9 read=1055 -> HashAggregate (cost=641898.44..641898.49 rows=3 width=20) (actual time=17338.481..17338.484 rows=7 loops=1) Buffers: shared hit=249850 read=77251 dirtied=501 written=159 -> Hash Anti Join (cost=597845.43..641450.61 rows=59711 width=20) (actual time=16142.147..17283.323 rows=63765 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=249850 read=77251 dirtied=501 written=159 -> Bitmap Heap Scan on customer (cost=2943.10..41911.98 rows=140605 width=24) (actual time=1588.817..2292.402 rows=190581 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{29,14,13,25,23,30,21}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=1395 read=70662 written=141 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2907.95 rows=140605 width=0) (actual time=1575.073..1575.073 rows=190581 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{29,14,13,25,23,30,21}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=23 read=36867 written=141 -> Hash (cost=406090.48..406090.48 rows=15104948 width=4) (actual time=14541.486..14541.486 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=248452 read=6589 dirtied=501 written=18 -> Seq Scan on orders (cost=0.00..406090.48 rows=15104948 width=4) (actual time=0.049..7197.113 rows=15015000 loops=1) Buffers: shared hit=248452 read=6589 dirtied=501 written=18 Total runtime: 17537.517 ms (30 rows) COMMIT; COMMIT