Solved

ip2location causes 2-3 second slow queries

Posted on 2008-06-11
13
867 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:ray-solomon
  • 7
  • 6
13 Comments
 
LVL 8

Expert Comment

by:shanikawm
ID: 21765856
Didn't you create an index for the table 'ip2location'?
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 21765887
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
 
LVL 8

Expert Comment

by:shanikawm
ID: 21765930
Try this

SELECT * FROM ip2location WHERE ipTO >= 3413882404 AND ipFROM <= 3413882404 LIMIT 1
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 21765955
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
 
LVL 10

Author Comment

by:ray-solomon
ID: 21765985
What would happen if I change the storage engine from MyISAM to Memory?

The space used for that table is 125.9 MB
0
 
LVL 8

Accepted Solution

by:
shanikawm earned 500 total points
ID: 21765987
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 8

Expert Comment

by:shanikawm
ID: 21766053
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 21766172
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
 
LVL 8

Expert Comment

by:shanikawm
ID: 21766219

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
 
LVL 10

Author Comment

by:ray-solomon
ID: 21766238
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
 
LVL 8

Expert Comment

by:shanikawm
ID: 21766251
Yes you are correct. :-). Hope you all problems are solved now.
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 21766260
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
 
LVL 10

Author Closing Comment

by:ray-solomon
ID: 31466429
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now