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 ('21', '30', '34', '14', '11', '31', '28') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('21', '30', '34', '14', '11', '31', '28') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694042.69..694042.70 rows=3 width=20) (actual time=11532.363..11532.365 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=234635 read=92240 written=1975 InitPlan 1 (returns $0) -> Aggregate (cost=52861.45..52861.46 rows=1 width=4) (actual time=557.325..557.325 rows=1 loops=1) Buffers: shared hit=35282 read=1059 written=50 -> Bitmap Heap Scan on customer customer_1 (cost=6637.61..51909.64 rows=380725 width=4) (actual time=99.266..477.571 rows=381535 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{21,30,34,14,11,31,28}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=35282 read=1059 written=50 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6542.43 rows=380725 width=0) (actual time=89.999..89.999 rows=381535 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{21,30,34,14,11,31,28}'::text[])) Buffers: shared hit=7 read=1056 written=50 -> HashAggregate (cost=641181.16..641181.20 rows=3 width=20) (actual time=11532.308..11532.310 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=234632 read=92240 written=1975 -> Hash Anti Join (cost=597216.47..640742.14 rows=58535 width=20) (actual time=11168.320..11512.235 rows=63664 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=234632 read=92240 written=1975 -> Bitmap Heap Scan on customer (cost=2906.63..41850.97 rows=139670 width=24) (actual time=621.546..844.694 rows=190749 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{21,30,34,14,11,31,28}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35195 Buffers: shared hit=70485 read=1597 written=85 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2871.71 rows=139670 width=0) (actual time=612.736..612.736 rows=190749 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{21,30,34,14,11,31,28}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35291 read=1596 written=85 -> Hash (cost=405686.04..405686.04 rows=15089904 width=4) (actual time=10438.610..10438.610 rows=15000000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658416kB Buffers: shared hit=164144 read=90643 written=1890 -> Seq Scan on orders (cost=0.00..405686.04 rows=15089904 width=4) (actual time=0.021..5100.697 rows=15000000 loops=1) Buffers: shared hit=164144 read=90643 written=1890 Planning time: 2.784 ms Execution time: 11537.331 ms (34 rows) COMMIT; COMMIT