![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, I'm using MySQL 5. I'm writing a simple app that finds the nearest store within a certain radius. So I'm using the query SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon – lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance` FROM `stores` HAVING `distance`<=’10′ ORDER BY `distance` ASC where $lat and $lon are the values I'm substituting based on user input. Although the columns lat and lon are indexed, I'm certain a full table scan is being performed because of the complexity of the query. Does anyone know a way to make the query more efficient? |
#3
| |||
| |||
|
|
Hi, I'm using MySQL 5. I'm writing a simple app that finds the nearest store within a certain radius. So I'm using the query SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon ??? lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance` FROM `stores` HAVING `distance`<=???10??? ORDER BY `distance` ASC where $lat and $lon are the values I'm substituting based on user input. Although the columns lat and lon are indexed, I'm certain a full table scan is being performed because of the complexity of the query. Does anyone know a way to make the query more efficient? |
#4
| |||
| |||
|
|
On Wed, 19 Jan 2011 09:28:27 -0800 (PST), laredotornado wrote: Hi, I'm using MySQL 5. I'm writing a simple app that finds the nearest store within a certain radius. So I'm using the query SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon ??? lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance` FROM `stores` HAVING `distance`<=???10??? ORDER BY `distance` ASC where $lat and $lon are the values I'm substituting based on user input. Although the columns lat and lon are indexed, I'm certain a full table scan is being performed because of the complexity of the query. Does anyone know a way to make the query more efficient? Sure. Scrap the radius search for a simpler means of determining "close", then if it really matters exactly how close the close ones are, perform the trig on only the ones that are close enough to matter. If you're looking for something at, for example, 45N 60W with a radius |
|
of 50 miles, do your math OUTSIDE the query and get $miles / 1.2 / (360 * 60) so your range is 45N-0.00193 to 45N+0.00193 for lat, and the same trick for lon. |
#5
| |||
| |||
|
|
How about selecting the stores (id, lat, lon) within a _square around your center point, first? That would be something like SELECT shopid, lat, lon FROM stores WHERE lat BETWEEN $cpointlat-10 AND $cpointlat+10 AND lon BETWEEN $cpointlon-10 AND $cpointlon+10 Now, you have a limited number of shops, found quickly, because this query will use your indices. If really necessary (wouldn't a square be sufficient already?) you can wrap this result with your circle calculation to obtain the shops that are within the square AND within the circle. |
#6
| |||
| |||
|
|
Hi, I'm using MySQL 5. Â*I'm writing a simple app that finds the nearest store within a certain radius. Â*So I'm using the query SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon – lon)* PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance` FROM `stores` HAVING `distance`<=’10′ ORDER BY `distance` ASC where $lat and $lon are the values I'm substituting based on user input. Â*Although the columns lat and lon are indexed, I'm certain a full table scan is being performed because of the complexity of the query. Â*Does anyone know a way to make the query more efficient? Thanks, - Dave |
#7
| |||
| |||
|
|
On Jan 19, 12:28 pm, laredotornado <laredotorn... (AT) zipmail (DOT) com> wrote: Hi, I'm using MySQL 5. I'm writing a simple app that finds the nearest store within a certain radius. So I'm using the query SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon – lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance` FROM `stores` HAVING `distance`<=’10′ ORDER BY `distance` ASC where $lat and $lon are the values I'm substituting based on user input. Although the columns lat and lon are indexed, I'm certain a full table scan is being performed because of the complexity of the query. Does anyone know a way to make the query more efficient? Thanks, - Dave Dave, to answer your theory as to the Full Table Scan, the answer is yes you are... Why? because you have not given it anything in a where clause to work with. Your query will go through EVERY row, calculate the "distance" and keep only those "HAVING" the value of <=10. Look at the other queries in this thread to see how they used the WHERE clause. You might also want to look at the SPATIAL extenions in MySQL Here is just one link with examples to get you started: http://dev.mysql.com/tech-resources/...ith-mysql.html |
![]() |
| Thread Tools | |
| Display Modes | |
| |