I am working with SQL server 2008
on query that looks for users within certain distance from central point that is moving - which means I am constantly hitting database (new results need to be added to the existing set and ones out of distance to be removed) and every millisecond saved is of value.
ATM we are using following query (Id's are used cause they are indexed ATM and good when trying for speed):
declare @lat int = 500,
@lon int = 700
SELECT @lat, @lon, id, journeyid, ((ACOS(SIN(@lat * PI() / 180) * SIN([Id] * PI() / 180) +
COS(@lat * PI() / 180) * COS([Id] * PI() / 180) * COS((@lon - [JourneyId]) *
PI() / 180)) * 180 / PI()) * 60 * 1.1515) as dist
FROM [OnlineLegal_dev1].[dbo].[Customer]
group by [Id], [JourneyId], ((ACOS(SIN(@lat * PI() / 180) * SIN([Id] * PI() / 180) +
COS(@lat * PI() / 180) * COS([Id] * PI() / 180) * COS((@lon - [JourneyId]) *
PI() / 180)) * 180 / PI()) * 60 * 1.1515)
HAVING ((ACOS(SIN(@lat * PI() / 180) * SIN([Id] * PI() / 180) +
COS(@lat * PI() / 180) * COS([Id] * PI() / 180) * COS((@lon - [JourneyId]) *
PI() / 180)) * 180 / PI()) * 60 * 1.1515)<=10000
ORDER BY ((ACOS(SIN(@lat * PI() / 180) * SIN([Id] * PI() / 180) +
COS(@lat * PI() / 180) * COS([Id] * PI() / 180) * COS((@lon - [JourneyId]) *
PI() / 180)) * 180 / PI()) * 60 * 1.1515) ASC
Current speed to select top 1k records is 00:00:00.097
How could I optimize this further for speed?