Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 stored procedure to return zip code distance

Author  Topic 

scantoria
Starting Member

15 Posts

Posted - 2009-12-24 : 16:09:06
I have a C# class that calculate distance between two zip codes. This worked well when my data was less than 10K. now that my data is almost 3M, the process is less than desirable. The application returns a list of zip codes if I ask to get all zip codes within 100 miles of point A.

I think if I create a stored procedure to perform the calculation, it will speed up my process. I have only created simple stored procedures and nothing this complex.

Please advice.


c# code
static double ZipDist(double Latitude1, double Longitude1, double Latitude2, double Longitude2)
{
double earthsRadius = 3956.087107103049;
double latitude1Radians = (Latitude1 / 180) * Math.PI;
double longitude1Radians = (Longitude1 / 180) * Math.PI;
double latitude2Radians = (Latitude2 / 180) * Math.PI;
double longitude2Radians = (Longitude2 / 180) * Math.PI;
double distance =
(earthsRadius * 2) *
Math.Asin(
Math.Sqrt(
Math.Pow(Math.Sin((latitude1Radians - latitude2Radians) / 2), 2) +
Math.Cos(latitude1Radians) *
Math.Cos(latitude2Radians) *
Math.Pow(Math.Sin((longitude1Radians - longitude2Radians) / 2), 2)
)
);

return distance;
}

Stephen Cantoria
scantoria@msn.com

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-24 : 16:23:50
You should consider putting this into a .NET CLR stored procedure.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

scantoria
Starting Member

15 Posts

Posted - 2009-12-24 : 17:14:42
I'd be happy to. Please provide link to .NET CLR stored procedure. I cant seem to find it.

Thanks.

quote:
Originally posted by tkizer

You should consider putting this into a .NET CLR stored procedure.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."



Stephen Cantoria
scantoria@msn.com
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-24 : 18:02:21
I'll show you one of mine as an example: http://www.tarakizer.com/files/scripts/DatabaseGrowth.zip

That is for a tool that I wrote and it is blogged here: http://weblogs.sqlteam.com/tarad/archive/2008/12/16/How-to-track-database-growth-across-multiple-SQL-Server-instances.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -