Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   HELP!!! Need Database Expert, Please (https://www.warriorforum.com/programming/1173422-help-need-database-expert-please.html)

Michael D Price 3rd April 2016 04:48 PM

HELP!!! Need Database Expert, Please
 
Hello All,

Wow this place has changed!

Anyway, down to business ...

I have a problem. I have developed an online script that will allow its users to perform a database search for the number of records within a radius of a given zipcode.

The results of this query must match (close enough) with the results of the same query using AccuZip software.

So far it functions exactly as it should, the way the calculations are performed, However it is too slow.

Currently taking over 9 seconds ...

Can someone take a look at the query, and make suggestions on how to improve upon it, to speed it up ...


-------------------
SELECT COUNT(*) as COUNT FROM (

SELECT unique_id, lat,lon,(((acos(sin((34.099800*pi()/180)) * sin((`lat`*pi()/180))+cos((34.099800*pi()/180)) * cos((`lat`*pi()/180)) * cos(((-118.412003- `Lon`)*pi()/180))))*180/pi())*60*1.1515) as distance FROM records)

as result WHERE distance <= 150;
-----------------------------

Any takers? the 150 is 150 mile radius, and the starting point is 34.099800,-118.412003

javrsmith 4th April 2016 09:08 AM

Re: HELP!!! Need Database Expert, Please
 
You need to partition the data faster. Right now it's calculating for every record, even those located thousands of miles away.

Are you able to partition faster? Perhaps you could add a clause to say "and substring('POSTAL',1,2) = '60'" This would let the database narrow down to the Chicago area right away. "98" for Seattle, etc.
If you don't have postal, you might want to roughly calculate it in a batch job and store with each record.

Alternatively, if the starting point is known when the record is created, you could store DISTANCE during the initial add. If the starting point is variable, this might not work. Maybe you have a limited number of starting points. You could store DISTANCE1, DISTANCE2, etc.

I also see that the calculation "34.099800*pi()/180" is being performed twice for this query. You could pre-calculate this into a constant variable. I would also use "3.14159" for pi, eliminating 6 calls to the function.

David Beroff 5th April 2016 03:50 AM

Re: HELP!!! Need Database Expert, Please
 
Continuing on javrsmith's idea, I'd precede the complex calculation with a simple check on the containing rectangle. i.e., If it's not in the rectangle, don't even bother doing the trig.


All times are GMT -6. The time now is 03:30 PM.