就像swasheck 提出的一个相关问题一样,我有一个历史上曾遇到过性能问题的查询。我正在查看 SSMS 上的查询计划并注意到Nested Loops (Inner Join)
警告:
没有连接谓词
基于一些草率的研究(激发信心的Scary DBA和Brent Ozar),看起来这个警告告诉我我的查询中有一个隐藏的笛卡尔积。我检查了我的查询几次,但没有看到交叉连接。这是查询:
DECLARE @UserId INT; -- Stored procedure input
DECLARE @Now DATETIME2(7) = SYSUTCDATETIME();
;WITH AggregateStepData_CTE AS -- Considering converting this CTE into an indexed view
(
SELECT
[UA].[UserId] -- FK to the UserId
, [UA].[DeviceId] -- FK to the push device's DeviceId (int)
, SUM(ISNULL([UA].[LatestSteps], 0)) AS [Steps]
FROM [User].[UserStatus] [UA]
INNER JOIN [User].[CurrentConnections] [M] ON
[M].[Monitored] = [UA].[UserId] AND [M].[Monitor] = @UserId
WHERE
[M].[ShareSteps] = 1 -- Only use step data if we are allowed to see.
AND
CAST([UA].[ReportedLocalTime] AS DATE) =
CAST(DATEADD(MINUTE, DATEPART(TZOFFSET, [UA].[ReportedLocalTime]), @Now) AS DATE)
-- Aggregate the steps for today based on the device's time zone.
GROUP BY
[UA].[UserId]
, [UA].[DeviceId]
)
SELECT
[UA].[UserId] -- FK to the UserId
, [UA].[ReportedLocalTime]
, CASE WHEN [M].[ShareLocation] = 1 THEN [UA].[Latitude] ELSE NULL END AS [Latitude]
, CASE WHEN [M].[ShareLocation] = 1 THEN [UA].[Longitude] ELSE NULL END AS [Longitude]
, CASE WHEN [M].[ShareLocation] = 1 THEN [UA].[LocationAccuracy] ELSE NULL END
AS [LocationAccuracy]
, CASE WHEN [M].[ShareSteps] = 1 THEN ISNULL([SD].[Steps], 0) ELSE NULL END AS [Steps]
, CASE WHEN [M].[ShareBattery] = 1 THEN [UA].[BatteryPercentage] ELSE NULL END
AS [BatteryPercentage]
, CASE WHEN [M].[ShareBattery] = 1 THEN [UA].[IsDraining] ELSE NULL END
AS [IsDraining]
, [PD].[DeviceName]
FROM [User].[LatestUserStatus] [UA]
INNER JOIN [User].[CurrentConnections] [M] WITH (NOEXPAND) ON
[M].[Monitored] = [UA].[UserId] AND [M].[Monitor] = @UserId
INNER JOIN [User].[PushDevice] [PD] ON [PD].[PushDeviceId] = [UA].[DeviceId]
LEFT JOIN [AggregateStepData_CTE] [SD] ON
[M].[Monitored] = [SD].[UserId] AND [SD].[DeviceId] = [UA].[DeviceId]
ORDER BY
[UA].[UserId]
, [UA].[ReportedLocalTime] DESC
可以在以下位置找到查询计划:https ://gist.github.com/anonymous/d6ac970b45eb75a88b99
还是我不应该像swasheck 问题中的结论那样害怕警告,毕竟估计的子树成本非常低,只有 0.05?
这个答案似乎也相关,这意味着这可能是 SQL Server 代表我进行的优化,因为它知道我可以删除连接。
这篇博文表明,嵌套循环没有谓词问题可能是由连接列上的 UDF 引起的。我没有在此查询中引用任何 UDF。
下面是CurrentConnections
视图的定义:
CREATE VIEW [User].[CurrentConnections]
WITH SCHEMABINDING
AS
SELECT
[M].[Monitor] -- FK to the UserId
, [M].[Monitored] -- FK to the UserId
, [M].[MonitoringId]
, [M].[ShareBattery]
, [M].[ShareLocation]
, [M].[ShareSteps]
, [M].[ShowInSocialFeed]
, [M].[Created] AS [RelationshipCreated]
, [AT].[AlertThresholdId]
, [AT].[EffectiveStartTime]
, [AT].[EndTime]
, [AT].[OverNightRedThreshold]
, [AT].[SendBatteryAlerts]
, [AT].[SendGeneralAlerts]
, [AT].[StartTime]
, [AT].[ThresholdInMinutes]
, [AT].[Threshold]
, [U_Monitored].[ProfilePhoto] AS [Monitored_ProfilePhoto]
, [U_Monitored].[DisplayName] AS [Monitored_DisplayName]
, [U_Monitored].[Fullname] AS [Monitored_FullName]
, [U_Monitored].[PhoneNumber] AS [Monitored_PhoneNumber]
FROM [User].[Monitoring] [M]
INNER JOIN [User].[AlertThreshold] [AT] ON [AT].[MonitoringId] = [M].[MonitoringId]
INNER JOIN [User].[User] [U_Monitored] ON [U_Monitored].[UserId] = [M].[Monitored]
WHERE
[M].[ArchivedOn] IS NULL
AND
[AT].[ArchivedOn] IS NULL
GO
CREATE UNIQUE CLUSTERED INDEX [IDX_User_CurrentConnections_Monitor_Monitored] ON
[User].[CurrentConnections]([Monitor], [Monitored]);
GO
CREATE NONCLUSTERED INDEX [IDX_User_CurrentConnections_Monitored] ON
[User].[CurrentConnections]([Monitored])
INCLUDE ([Monitor], [ShareBattery], [ShareLocation], [ShareSteps]);
在我的 CTE 中,我缺少
WITH (NOEXPAND)
查询提示。一旦我添加了这个查询提示,没有谓词的连接就从我的查询计划中消失了。我有过类似的情况,不久前读到引擎将确定连接算法的类型是否最有效,有时会放弃连接谓词以代表编写代码的效率。通过不展开,你告诉引擎不要下降到视图并坚持你的顶层查询块。
如果提出更多建议来证明您拥有连接谓词,请不要被劝阻。也请相信我,我检查了 100 次,我发现它们是正确的。哈哈。因此我的贡献。
尝试在表引用堆栈中上下移动 Join,确保别名引用的依赖顺序正确。这意味着如果你有 4 个连接,我已经成功并改变了一些事情,将引用较少或选择性较低的表连接移动到连接的最后一个位置,反之亦然。
确保统计信息和索引是最新的。因为我对 MS 应该执行的“自动创建/更新统计信息”自动计划和算法没有信心,所以我偶尔会发出以下语句以确保它们最近得到处理。
Sp_UpdateStats Sp_CreateStats 'indexonly'
强制提示应始终进行测试和记录,但我想提醒并告知我发生了什么以及我的调查揭示了什么。
以下链接是与同一事物相关的线程...检查“超时找到足够好的计划”,以及属性对话框中的其他警告。
SQL Central 类似线程
如果有任何想法可以像对我一样解决您的问题,我很感兴趣。