首页 > > > IP地址段查询深度优化案例
select nation,province,cityfrom ip_idcwhere ip_start <='113.201.214.203'::inet andip_end >='113.201.214.203'::inet;
postgres=# \d ip_idcTable "public.ip_idc"Column | Type | Collation | Nullable | Default--------------------+------------------------+-----------+----------+---------id | character varying(255) | | not null |ip_start | inet | | |ip_end | inet | | |nation | character varying(255) | | |province | character varying(255) | | |city | character varying(255) | | |...(略)Indexes:"ip_idc_pkey" PRIMARY KEY, btree (id)"ip_idc_ip_start_ip_end_idx" btree (ip_start, ip_end)
postgres=# explain (analyze ,buffers)select nation,province,city from ip_idc where ip_start <='113.201.214.203'::inet and ip_end >='113.201.214.203'::inet;QUERY PLAN-----------------------------------------------------------------------------------------------------------------Seq Scan on ip_idc (cost=0.00..128204.08 rows=645500 width=29) (actual time=1062.045..1066.116 rows=1 loops=1)Filter: ((ip_start <= '113.201.214.203'::inet) AND (ip_end >= '113.201.214.203'::inet))Rows Removed by Filter: 3470145Buffers: shared hit=4143 read=72010I/O Timings: read=321.196Planning time: 17.140 msExecution time: 1073.907 ms(7 rows)
postgres=# explain (analyze ,buffers)select nation,province,city from ip_idc where ip_start <='60.201.214.203'::inet and ip_end >='60.201.214.203'::inet;QUERY PLAN-------------------------------------------------------------------------------------------------------------------------------------------------Index Scan using ip_idc_ip_start_ip_end_idx on ip_idc (cost=0.43..77943.75 rows=640787 width=29) (actual time=104.813..104.815 rows=1 loops=1)Index Cond: ((ip_start <= '60.201.214.203'::inet) AND (ip_end >= '60.201.214.203'::inet))Buffers: shared hit=3240Planning time: 0.082 msExecution time: 104.843 ms(5 rows)postgres=# explain (analyze ,buffers)select nation,province,city from ip_idc where ip_start <='10.201.214.203'::inet and ip_end >='10.201.214.203'::inet;QUERY PLAN--------------------------------------------------------------------------------------------------------------------------------------------Index Scan using ip_idc_ip_start_ip_end_idx on ip_idc (cost=0.43..17828.34 rows=30263 width=29) (actual time=2.298..2.299 rows=1 loops=1)Index Cond: ((ip_start <= '10.201.214.203'::inet) AND (ip_end >= '10.201.214.203'::inet))Buffers: shared hit=70Planning time: 0.156 msExecution time: 2.327 ms(5 rows)postgres=# explain (analyze ,buffers)select nation,province,city from ip_idc where ip_start <='1.201.214.203'::inet and ip_end >='1.201.214.203'::inet;QUERY PLAN------------------------------------------------------------------------------------------------------------------------------------------Index Scan using ip_idc_ip_start_ip_end_idx on ip_idc (cost=0.43..3426.17 rows=5057 width=29) (actual time=0.392..0.393 rows=1 loops=1)Index Cond: ((ip_start <= '1.201.214.203'::inet) AND (ip_end >= '1.201.214.203'::inet))Buffers: shared hit=15Planning time: 0.103 msExecution time: 0.413 ms(5 rows)
create type inetrange as range(subtype=inet)
create index on ip_idc using gist(inetrange(ip_start, ip_end, '[]'::text))
postgres=# explain (analyze,buffers) select * from ip_idc where inetrange(ip_start,ip_end,'[]') @> '113.21.214.203'::inet;QUERY PLAN-------------------------------------------------------------------------------------------------------------------------------Bitmap Heap Scan on ip_idc (cost=1.92..3.44 rows=1 width=136) (actual time=11.071..11.072 rows=1 loops=1)Recheck Cond: (inetrange(ip_start, ip_end, '[]'::text) @> '113.21.214.203'::inet)Heap Blocks: exact=1Buffers: shared hit=521-> Bitmap Index Scan on ip_idc_inetrange_idx (cost=0.00..1.92 rows=1 width=0) (actual time=11.066..11.066 rows=1 loops=1)Index Cond: (inetrange(ip_start, ip_end, '[]'::text) @> '113.21.214.203'::inet)Buffers: shared hit=520Planning time: 0.098 msExecution time: 11.140 ms(9 rows)
create extension ip4r;create table ip_idc2(id serial,ip_start inet,ip_end inet,iprange inet,iprange2 ip4r);insert into ip_idc2(ip_start,ip_end,iprange,iprange2)select (r1::text ||'.'|| r2::text ||'.'|| r3::text||'.0')::inet,(r1::text ||'.'|| r2::text ||'.'|| r3::text||'.255')::inet,(r1::text ||'.'|| r2::text ||'.'|| r3::text||'.0/24')::inet,(r1::text ||'.'|| r2::text ||'.'|| r3::text||'.0/24')::ip4rfrom generate_series(1,60) a(r1),generate_series(1,254) b(r2),generate_series(1,254) c(r3) limit 1;create index on ip_idc2 using gist(inetrange(ip_start, ip_end, '[]'::text));create index on ip_idc2 using gist(iprange inet_ops);create index on ip_idc2 using gist(iprange2);
postgres=# explain (analyze,buffers) select * from ip_idc2 where inetrange(ip_start,ip_end,'[]') @> '33.21.214.203'::inet;QUERY PLAN--------------------------------------------------------------------------------------------------------------------------------------Bitmap Heap Scan on ip_idc2 (cost=384.42..17950.58 rows=19355 width=33) (actual time=15.131..15.133 rows=1 loops=1)Recheck Cond: (inetrange(ip_start, ip_end, '[]'::text) @> '33.21.214.203'::inet)Heap Blocks: exact=1Buffers: shared hit=668Bitmap Index Scan on ip_idc2_inetrange_idx (cost=0.00..379.58 rows=19355 width=0) (actual time=15.122..15.122 rows=1 loops=1)Index Cond: (inetrange(ip_start, ip_end, '[]'::text) @> '33.21.214.203'::inet)Buffers: shared hit=667Planning time: 0.072 msExecution time: 15.204 msrows)postgres=# explain (analyze,buffers) select * from ip_idc2 where iprange >>= '33.21.214.203'::inet;QUERY PLAN------------------------------------------------------------------------------------------------------------------------------Index Scan using ip_idc2_iprange_idx on ip_idc2 (cost=0.41..3.43 rows=1 width=33) (actual time=2.758..4.940 rows=1 loops=1)Index Cond: (iprange >>= '33.21.214.203'::inet)Buffers: shared hit=227Planning time: 0.082 msExecution time: 4.964 msrows)postgres=# explain (analyze,buffers) select * from ip_idc2 where iprange2 >>= '33.21.214.203'::ip4r;QUERY PLAN---------------------------------------------------------------------------------------------------------------------------------Bitmap Heap Scan on ip_idc2 (cost=54.42..4966.41 rows=3871 width=33) (actual time=0.032..0.032 rows=1 loops=1)Recheck Cond: (iprange2 >>= '33.21.214.203'::ip4r)Heap Blocks: exact=1Buffers: shared hit=4Bitmap Index Scan on ip_idc2_iprange2_idx (cost=0.00..53.45 rows=3871 width=0) (actual time=0.027..0.027 rows=1 loops=1)Index Cond: (iprange2 >>= '33.21.214.203'::ip4r)Buffers: shared hit=3Planning time: 0.083 msExecution time: 0.065 msrows)
create index on ip_idc2(ip_start);
postgres=# explain (analyze ,buffers)select * from ip_idc2 where ip_start <='33.201.214.203'::inet and ip_end >='33.201.214.203'::inet order by ip_start desc limit 1;QUERY PLAN-------------------------------------------------------------------------------------------------------------------------------------------------------Limit (cost=0.43..0.53 rows=1 width=33) (actual time=0.019..0.020 rows=1 loops=1)Buffers: shared hit=4-> Index Scan Backward using ip_idc2_ip_start_idx on ip_idc2 (cost=0.43..99888.39 rows=957385 width=33) (actual time=0.018..0.018 rows=1 loops=1)Index Cond: (ip_start <= '33.201.214.203'::inet)Filter: (ip_end >= '33.201.214.203'::inet)Buffers: shared hit=4Planning time: 0.133 msExecution time: 0.044 ms(8 rows)
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69917105/viewspace-3150559/ ,如需转载,请注明出处,否则将追究法律责任。