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 ('34', '30', '24', '31', '14', '12', '29') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('34', '30', '24', '31', '14', '12', '29') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=695284.69..695284.69 rows=3 width=20) (actual time=17287.223..17287.224 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=319439 read=8183 dirtied=22 written=6 InitPlan 1 (returns $0) -> Aggregate (cost=52885.37..52885.38 rows=1 width=4) (actual time=693.495..693.495 rows=1 loops=1) Buffers: shared hit=35286 read=1056 -> Bitmap Heap Scan on customer (cost=6641.83..51931.84 rows=381410 width=4) (actual time=145.126..577.092 rows=381090 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{34,30,24,31,14,12,29}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=35286 read=1056 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6546.48 rows=381410 width=0) (actual time=132.903..132.903 rows=381090 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{34,30,24,31,14,12,29}'::text[])) Buffers: shared hit=8 read=1056 -> HashAggregate (cost=642399.24..642399.28 rows=3 width=20) (actual time=17287.151..17287.155 rows=7 loops=1) Buffers: shared hit=319437 read=8183 dirtied=22 written=6 -> Hash Anti Join (cost=598992.86..641954.30 rows=59325 width=20) (actual time=16588.033..17259.487 rows=63741 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=319437 read=8183 dirtied=22 written=6 -> Bitmap Heap Scan on customer (cost=2907.92..41858.35 rows=139902 width=24) (actual time=770.151..1064.707 rows=190530 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{34,30,24,31,14,12,29}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=65218 read=6851 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2872.95 rows=139902 width=0) (actual time=760.016..760.016 rows=190530 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{34,30,24,31,14,12,29}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35296 read=1594 -> Hash (cost=406897.75..406897.75 rows=15134975 width=4) (actual time=15807.102..15807.102 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=254216 read=1332 dirtied=22 written=6 -> Seq Scan on orders (cost=0.00..406897.75 rows=15134975 width=4) (actual time=0.018..5791.991 rows=15015000 loops=1) Buffers: shared hit=254216 read=1332 dirtied=22 written=6 Total runtime: 17417.842 ms (30 rows) COMMIT; COMMIT