Solved

Improving a query (peformance)

Posted on 2013-01-30
3
279 Views
Last Modified: 2013-01-30
Hello,

I have the following query (great circle computation):

SELECT id, city_name, ( 6371 * acos( cos( radians(37.102528) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(25.376093) ) + sin( radians(37.102528) ) * sin( radians( lat ) ) ) ) AS distance
FROM city_names HAVING distance < 25 OR distance IS NULL ORDER BY distance LIMIT 0 , 200;

My table has about 15M records and this query takes from 13 to 22 sec to execute. I have an index on lat and long fields (both fields on the same index).

Are there any ways to improve the speed of this query?
0
Comment
Question by:infodigger
3 Comments
 
LVL 10

Assisted Solution

by:deviprasadg
deviprasadg earned 166 total points
Comment Utility
create materialized view with the above query for sharp increase in performance of the select statement.

http://stackoverflow.com/questions/2534506/how-to-implement-materialized-view-with-mysql
0
 
LVL 7

Accepted Solution

by:
Beneford earned 167 total points
Comment Utility
Do you have a small number of locations from which the distance is calculated?
If so, an additional column in the table for each destination would allow you to pre-calculate (in 22s) and then run the calculation.

If there are multiple centre points, you should see an improvement by:
1. Precalculate radians ( fixed-value ) - just multiply by pi/180.
2. Precalculate sin(radians(lat)) - (same for cos and lng) and store the values in the table
3. Move the Earth-size multiplier (6371) into the having clause: HAVING distance < 25/6371

Note: if you're looking at small distances, you may want to use the Haversine formula, but that may not be quicker.

Better (but I have no experience of it) might be the MySQL 5 Spacial Extensions - http://dev.mysql.com/doc/refman/5.0/en/spatial-extensions.html.
0
 
LVL 24

Assisted Solution

by:Tomas Helgi Johannsson
Tomas Helgi Johannsson earned 167 total points
Comment Utility
Hi!

For this query to have optimal performance you should create an index like this
CREATE INDEX city_dist_ix ON TABLE city_names (lat,long, city_name,id)  USING BTREE;

This will make the query index only query and hopefully a little bit faster. :)

Regards,
     Tomas Helgi
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

7 Experts available now in Live!

Get 1:1 Help Now