If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > improve performance of latitude/longitude calculation (was "Huge database problem")

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-22-06, 04:54
beniwtv beniwtv is offline
Registered User
 
Join Date: Mar 2006
Posts: 1
Question improve performance of latitude/longitude calculation (was "Huge database problem")

Hi. I am a really newbie in MySQL. I have a database with 8.500.000 (aprox.) rows and
I want to select a row like this:


SELECT *,((DEGREES(ACOS((SIN(RADIANS(36.517)) * SIN(RADIANS(f4)) + COS(RADIANS(36.517)) * COS(RADIANS(f4)) * COS(RADIANS((-4.883)) - RADIANS(f5)))))*69.09)*1.6) as km FROM locdb order by km limit 0,1


(I've divided the lines for better reading). The syntax selects the nearest city acording to the latitude (36.517) and longitude (-4.883). The latiude and longitude colums on my table are f4 and f5, respectively.


The problem is that (obviously) it takes about a minute to do it. Has anyone an idea how to improve the table or the syntax to get a better results?

Thanks.
Reply With Quote
  #2 (permalink)  
Old 03-22-06, 09:38
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,258
this may be a case for ignoring one of the fundamental tents of normlisation - perhaps it would nake sense to store your intermediate values int he table. after all its unlikely they are going to change from read to read (unless your cities are on mud slide or earthquake zones then its unlilkely tyhey are going to move).

index these intermediate columns.

not knowing what your application is you could also reduce the number of rows handled by the calculation by say imposing a crude filter
ie if you are looking for
latitude (36.517)
longitude (-4.883).
then filter out the dreck by say returning rows with a latitude of 36.517 +/- n
and a longitude of -4.883 +/- n
-this wouldonly work if you are looking for a limited subset of data, obviuosly if you need all 8.5million rows to be examined then thats not going to do much good.

anything you can do to reduce the amount of work the trig fucntions the server has to do has got to be a bonus

Not knowing how the MySQL parser handles trig it may be better to pass what you can to the parser as numbers rather than function calls (for example it may by calulating the trig function each time - doubt it but y'never know [might be worth a try first off an see what difference if any it makes)

HTH
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On