Hi,
I have recently purchased the ip2location database. This databse containts 1.5million rows with IP ranges (ipFROM,ipTO) and some other data like countryname, cityname etc. I need to write a query that is extremely much faster than my current ones.
The IP numbers are stored in long format (int(10) unsigned). The SELECT statement provided by ip2location is extremely slow, and looks like this:
SELECT countryname FROM tbl WHERE (ipFROM <= IP) AND (ipTO >= IP)
I currently do the following:
1. SELECT max(ipFROM) FROM tbl WHERE (ipFROM <= IP)
2. SELECT countryname FROM tbl WHERE (ipFROM = RESULT FROM 1)
This speeds up the query with about 50%, but a query for a high ip-number - ie. 212.78.161.106 still takes about 4 seconds to process (7,5 seconds with original SELECT).
A friend (knowing a lot about mathematics, but little about databases, suggested the following;
Consider IP to be the address we are searching for. The database consists of N elements. N0=1
1. Compare IP with record N/2
2. if IP < N/2 -> N = N/2
if IP > N/2 -> N0 = N/2
if none of these, adress matches.
3. next iteration compare IP with NO/2+NO/2
4. continue this process until none match, and you have the adress..
MY QUESTION: How can I do this? I use php and mysql.
I would also appriciate any other suggestions to speed up the query.
Sero
I have recently purchased the ip2location database. This databse containts 1.5million rows with IP ranges (ipFROM,ipTO) and some other data like countryname, cityname etc. I need to write a query that is extremely much faster than my current ones.
The IP numbers are stored in long format (int(10) unsigned). The SELECT statement provided by ip2location is extremely slow, and looks like this:
SELECT countryname FROM tbl WHERE (ipFROM <= IP) AND (ipTO >= IP)
I currently do the following:
1. SELECT max(ipFROM) FROM tbl WHERE (ipFROM <= IP)
2. SELECT countryname FROM tbl WHERE (ipFROM = RESULT FROM 1)
This speeds up the query with about 50%, but a query for a high ip-number - ie. 212.78.161.106 still takes about 4 seconds to process (7,5 seconds with original SELECT).
A friend (knowing a lot about mathematics, but little about databases, suggested the following;
Consider IP to be the address we are searching for. The database consists of N elements. N0=1
1. Compare IP with record N/2
2. if IP < N/2 -> N = N/2
if IP > N/2 -> N0 = N/2
if none of these, adress matches.
3. next iteration compare IP with NO/2+NO/2
4. continue this process until none match, and you have the adress..
MY QUESTION: How can I do this? I use php and mysql.
I would also appriciate any other suggestions to speed up the query.