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
You could do worse than look at the
GEOGRAPHYdatatype, for example:geography::Pointtakes the latitude and longitude as well as an SRID (Special Reference ID number). In this case, the SRID is 4326 which is standard latitude and longitude. As you already have latitude and longitude, you can justALTER TABLEto add the geography column thenUPDATEto populate it.I’ve shown two ways to get the data out of the table, however you can’t create an indexed view with this (indexed views can’t have self-joins). You could though create a secondary table that is effectively a cache, that’s populated based on the above. You then just have to worry about maintaining it (could be done through triggers or some other process).
Note that the cross join will give you 250,000,000,000 rows, but searching is simple as you only need look at one of the places columns (i.e.,
SELECT * FROM table WHERE Place1 = 'Sheffield' AND distance < 100, the second will give you significantly less rows, but the query then needs to consider both the Place1 and Place2 column).