0

我有以下 sql 查询。

declare @RoleID int
SELECT @RoleID = RoleID FROM RoleMaster WHERE RoleEnglishDesc = 'Surveyor'

SELECT * FROM (
 SELECT
  S.ShiftID, 2 AS Flag, ISNULL(U.Latitude, 0) Latitude, ISNULL(U.Longitude, 0) Longitude,
  U.EmployeeEnglishName AS ToolTipEnglish, U.EmployeeArabicName AS ToolTipArabic,
  0 AS CaseStageID, COUNT(C.SurveyorAssigned) AS Assigned
  -- L.EntryTime AS LastEntryTime
  -- S1.cnt AS OnBreak
 FROM 
  Users U
   INNER JOIN dbo.SurveyorShift S ON S.UserId = U.UserID AND S.ShiftStatus = 1
   LEFT JOIN CaseInfoCommandQueue C ON C.SurveyorAssigned = U.UserID
 WHERE U.IsActive = 1
  AND NULLIF(NULLIF(U.Latitude, '0'), '') IS NOT NULL
  AND NULLIF(NULLIF(U.Longitude, '0'), '') IS NOT NULL
  AND ((U.RoleID = @RoleID) OR (U.RoleID2 = @RoleID) OR (U.RoleID3 = @RoleID))
 GROUP BY U.UserID, RoleID, Latitude, Longitude, EmployeeEnglishName, EmployeeArabicName, ShiftID
) D
OUTER APPLY 
(
 SELECT TOP 1 EntryTime FROM DeviceUserLocation DLoc
 WHERE DLoc.ShiftID = D.ShiftID ORDER BY DLoc.EntryTime DESC
) AS L

我在最后一个外部应用的唯一问题是获取入口时间。因为外部应用只需要 10 秒。该表 DeviceUserLocation 也有一些条目。以其他方式使用前 1 的任何克服或步骤?

4

1 回答 1

0

为您的数据创建覆盖索引,确保您在每个表上都有一个聚集索引,并且如上所述重新键,如果您不知道聚集索引和覆盖索引,那么在这里:http ://en.wikipedia.org/wiki/Database_index#Covering_index尤其是在这里:https ://www.simple-talk.com/sql/learn-sql-server/using-covering-indexes-to-improve-query-performance/您的查询可能正在进行表扫描(搜索或检查每个行)而不是索引查找:在索引中找到一个值并直接转到该行。(如果该列上没有和索引,则无法进行索引查找,如果没有聚集索引,则无法直接进行)

于 2013-03-19T09:38:09.173 回答