geocoding - The most efficient way to query ip ranges in mysql -
i have geoencoding database ranges of integers (ip addresses equivalent) in each row fromip
(long) toip
(long). integers created ip addresses php ip2long
i need find row in given ip address (converted long) within range.
what efficient way it? (keys , query)
if (the naive solution) select * ipranges fromip <= givenip , toip >= givenip limit 1
, key fromip, toip
. case ip address not in given ranges search goes through rows.
some more info:
explain select * ipranges ipfrom <= 2130706433 , ipto >= 2130706433 order ipfrom asc limit 1|
gives me 2.5m rows (total 3.6m in table). key is:
primary key (
ipfrom
,ipto
)
that not seem efficient @ all. (the ip above in none of ranges)
your query fine, put index on (fromip, toip) covering index query. table won't have examined @ all, sorted index gets searched, fast can be.
the search not go through rows. not go through none of rows, index, won't examine every entry in index either. index stored sorted tree, , 1 path through tree have followed determine ip not in table.
Comments
Post a Comment