I have a huge database of businesses (about 500,000) with zipcode, address etc . I need to display them by ascending order from 100 miles are of users zipcode. I have a table for zipcodes with related latitude and longitude. What will be faster/better solution ?
Case 1: to calculate distance and sort by distance. I will have users current zipcode, latitude and longitude in session. I will calculate distance using a SQL Server function.
Case 2: to get all zipcodes in 50 miles area and get businesses with all those zipcodes. Here I will have to write a select in nested query while finding businesses.
I think case 1 will calculate distance for all businesses in database. While 2nd case will just fetch zipcodes and will end up fetching only required businesses. Hence case 2 should be better? I would appreciate any suggestion here.
Here is LINQ query I have for case 1.
var businessListQuery = (from b in _DB.Businesses
let distance = _DB.CalculateDistance(b.Zipcode,userLattitude,userLogntitude)
where b.BusinessCategories.Any(bc => bc.SubCategoryId == subCategoryId)
&& distance < 100
orderby distance
select new BusinessDetails(b, distance.ToString()));
int totalRecords = businessListQuery.Count();
var ret = businessListQuery.ToList().Skip(startRow).Take(pageSize).ToList();
On a side note app is in C# .
Thanks