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 ('18', '20', '31', '16', '23', '34', '15') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('18', '20', '31', '16', '23', '34', '15') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694114.06..694114.06 rows=3 width=20) (actual time=11659.709..11659.710 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=246764 read=80081 written=105 InitPlan 1 (returns $0) -> Aggregate (cost=52924.02..52924.03 rows=1 width=4) (actual time=860.094..860.094 rows=1 loops=1) Buffers: shared hit=22124 read=14221 written=57 -> Bitmap Heap Scan on customer customer_1 (cost=6650.90..51967.92 rows=382439 width=4) (actual time=97.175..785.627 rows=382560 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,20,31,16,23,34,15}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=22124 read=14221 written=57 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6555.29 rows=382439 width=0) (actual time=88.323..88.323 rows=382560 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{18,20,31,16,23,34,15}'::text[])) Buffers: shared hit=8 read=1059 written=1 -> HashAggregate (cost=641189.95..641190.00 rows=3 width=20) (actual time=11659.652..11659.653 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=246761 read=80081 written=105 -> Hash Anti Join (cost=597219.84..640743.82 rows=59484 width=20) (actual time=11275.862..11639.515 rows=63551 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=246761 read=80081 written=105 -> Bitmap Heap Scan on customer (cost=2910.00..41863.00 rows=140000 width=24) (actual time=922.506..1157.863 rows=191313 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,20,31,16,23,34,15}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35158 Buffers: shared hit=57292 read=14760 written=58 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2875.00 rows=140000 width=0) (actual time=913.698..913.698 rows=191313 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,20,31,16,23,34,15}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=22134 read=14760 written=58 -> Hash (cost=405686.04..405686.04 rows=15089904 width=4) (actual time=10231.180..10231.180 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=189466 read=65321 written=47 -> Seq Scan on orders (cost=0.00..405686.04 rows=15089904 width=4) (actual time=0.020..4795.034 rows=15015000 loops=1) Buffers: shared hit=189466 read=65321 written=47 Planning time: 2.914 ms Execution time: 11672.169 ms (34 rows) COMMIT; COMMIT