ip2location causes 2-3 second slow queries

I use an ip2location geolocation database from ip2location.com on a heavily used server.

I enabled the slow query log to track performance and it is taking 2-3 seconds to finish a query.
I ran an explain on a particular query an it shows that is has to scan all 3 million+ rows.
The query appears to not be optimized for speed.
I am using the example sql query as recommended on their website.

SELECT * FROM ip2location WHERE 3413882404 <= ipTO AND 3413882404>=ipFROM

Does anyone know how to speed this up dramatically?

We have a powerful server and even though the queries take a long time to execute, our server resources still appear to be at minimal usage.

Dell Poweredge 2950 rack
64-bit CentOS 5
2 x quad core xeon E5335 @ 2.00GHz
4 x SAS hard drive's - raid 10
4GB ram
CREATE TABLE IF NOT EXISTS `ip2location` (
  `ipFROM` int(10) unsigned zerofill NOT NULL default '0000000000',
  `ipTO` int(10) unsigned zerofill NOT NULL default '0000000000',
  `countrySHORT` char(2) NOT NULL default '',
  `countryLONG` varchar(64) NOT NULL default '',
  `ipREGION` varchar(128) NOT NULL default '',
  `ipCITY` varchar(128) NOT NULL default '',
  PRIMARY KEY  (`ipFROM`,`ipTO`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
 
Example record:
 
ipFROM 	     ipTO         countrySHORT  countryLONG     ipREGION        ipCITY
0050331648   0050331903   US            UNITED STATES   MASSACHUSETTS   BEVERLY
 
 
 
 
Here is basically how it is implemented on the site using PHP:
 
<?php
 
function Dot2LongIP ($IPaddr) 
{
	if ($IPaddr == "0.0.0.0") 
	{
		return 0;
	} 
	else 
	{
		$ips = split ("\.", "$IPaddr");
		return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
	}
}
 
//Convert the ip to a number
$Longip = Dot2LongIP('203.123.182.36');
 
// Execute SQL query (recommended query from ip2location.com)
echo "SELECT * FROM ip2location WHERE $Longip <= ipTO AND $Longip>=ipFROM";
 
 
?>

Open in new window

LVL 10
ray-solomonAsked:
Who is Participating?
 
shanikawmConnect With a Mentor Commented:
Refer following page. Similar solution is there. But that table has an index for only one column. Not a shared one.

http://forums.mysql.com/read.php?24,72255,72255
0
 
shanikawmCommented:
Didn't you create an index for the table 'ip2location'?
0
 
ray-solomonAuthor Commented:
There is a single shared index:

ipFROM, ipTO

BTW, I was going to edit the post.
I have to taken another look at the CPU/Memory/MySQL usage, and it went from about 5% usage to 14% usage in just a couple of hours.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
shanikawmCommented:
Try this

SELECT * FROM ip2location WHERE ipTO >= 3413882404 AND ipFROM <= 3413882404 LIMIT 1
0
 
ray-solomonAuthor Commented:
The LIMIT helped a little and had to scan 2271177 rows instead of 3037152 rows.

But it still took a long time. about 7 seconds
0
 
ray-solomonAuthor Commented:
What would happen if I change the storage engine from MyISAM to Memory?

The space used for that table is 125.9 MB
0
 
shanikawmCommented:
0
 
ray-solomonAuthor Commented:
I followed the explanation on that page and experimented myself and came up with great results from the mysql forum.
I played around with the indexes and found that one index was needed (ipTO).

The query that ip2location recommended. (DO NOT USE):
SELECT * FROM ip2location WHERE 3413882404 <= ipTO AND 3413882404>=ipFROM

This query will pull a result in about 0.0003 to 1 second (only use ipTO for an index)
SELECT * FROM ip2location WHERE ipTO >= 3413882404 AND ipFROM <= 3413882404 LIMIT 1

Below is the fastest query:

This query will pull a result in about ~ 0.0001 seconds (only use ipTO for an index)
SELECT * FROM ip2location WHERE 3413882404 <= ipTO LIMIT 1

Just FYI, I have query caching enabled, since I cannot turn it off, I used different IP's instead which makes it a different query than what is in the cache.

I am going to restart mysql and watch the server resources. It already jumped to almost 18% before now.

Here are the three corresponding code examples that I used.
 
//echo "SELECT * FROM ip2location WHERE $Longip <= ipTO AND $Longip>=ipFROM";
//echo "SELECT * FROM ip2location WHERE ipTO >= $Longip AND ipFROM <= $Longip LIMIT 1";
//echo "SELECT * FROM ip2location WHERE $Longip <= ipTO LIMIT 1";

Open in new window

0
 
shanikawmCommented:

OK.

FYI, I think you can use query like follow to avoid the cache,

SELECT SQL_NO_CACHE FROM ip2location WHERE 3413882404 <= ipTO LIMIT 1.
0
 
ray-solomonAuthor Commented:
Thanks for that, yes, I used that query, but I had to modify it.
You missed the column names to select from.
:)

I am selecting all columns in my situation. It returns the result in 0.0002 seconds.
SELECT SQL_NO_CACHE * FROM ip2location WHERE 3413882404 <= ipTO LIMIT 1
0
 
shanikawmCommented:
Yes you are correct. :-). Hope you all problems are solved now.
0
 
ray-solomonAuthor Commented:
Yes, Thank you much. I hope this post will be very helpful to others.
There must be a lot of people using ip2location databases.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.