我有以下的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也有大量条目。任何克服或步骤以其他方式使用top 1?
答案 0 :(得分:0)
为您的数据创建覆盖索引,确保您在每个表上都有聚簇索引,如上所述,如果您不了解聚簇和覆盖索引,请在此处http://en.wikipedia.org/wiki/Database_index#Covering_index,尤其是:{{3您的查询可能正在进行表扫描(搜索或检查每一行)而不是索引查找:在索引中查找值并直接转到该行。 (如果该列没有和索引,则无法进行索引查找,如果没有聚簇索引,则无法直接进行索引)