Pic*_*llo 4 performance sql-server execution-plan sql-server-2016 query-performance
我正在尝试更新使用子句谓词中的IN运算符的查询,以比较潜在的性能改进并更好地了解当两者互换时幕后发生的事情。这是我的理解,在实践中,查询优化器对待,并以同样的方式时,它可以。WHEREEXISTSEXISTSIN
我注意到当使用IN运算符运行查询时,它返回所需的结果集。但是,当我用EXISTS等效项替换它时,它会从我要过滤的主表中提取所有值。它忽略传递给EXISTS (SELECT ...并返回所有可能的不同值的提供的输入值。
用例相对简单:查询接受一个管道分隔的@Series字符串,最多可以包含 4 个值,例如S1|S2|S3|S4或S2|S4。在这里,我string_split将输入到一个表中的变量@SeriesSplit,以确定相应的内部[SeriesId]的[Series]。然后过滤返回的结果集以排除[Series]未通过的结果。
为了说明,这里有一个类似的表定义:
DROP TABLE IF EXISTS [dbo].[Document]
IF OBJECT_ID('[dbo].[Document]', 'U') IS NULL
BEGIN
CREATE TABLE [dbo].[Document] (
[DocumentId] bigint IDENTITY(1,1) NOT NULL
,[DocumentSeriesId] [tinyint] NOT NULL
,CONSTRAINT [PK_Document] PRIMARY KEY CLUSTERED ([DocumentId] ASC)
,INDEX [IX_Document_SeriesId] NONCLUSTERED ([DocumentSeriesId] ASC)
);
END;
GO
Run Code Online (Sandbox Code Playgroud)
用虚拟数据填充测试表。
SET IDENTITY_INSERT [dbo].[Document] ON;
;WITH [DocumentSeed] AS (
SELECT
1 AS [DocumentId]
UNION ALL
SELECT
[DocumentId] + 1
FROM
[DocumentSeed]
WHERE
[DocumentId] < 2048)
INSERT INTO [dbo].[Document] ([DocumentId], [DocumentSeriesId])
SELECT
[DocumentId]
,ABS(CHECKSUM(NEWID()) % 4) + 1
FROM
[DocumentSeed] OPTION (MAXRECURSION 2048);
SET IDENTITY_INSERT [dbo].[Document] OFF;
Run Code Online (Sandbox Code Playgroud)
首先,使用IN运算符并返回所需结果的查询。
-- Specify the Document Series to be returned.
DECLARE @Series varchar(12) = 'S1|S2'
-- Split the user input and insert it into a table variable.
DECLARE @SeriesSplit table ([SeriesId] tinyint, [Series] varchar(2))
BEGIN
INSERT INTO @SeriesSplit ([SeriesId], [Series])
SELECT
CASE [value] WHEN 'S1' THEN 1 WHEN 'S2' THEN 2 WHEN 'S3' THEN 3 WHEN 'S4' THEN 4 ELSE 5 END AS [SeriesId]
,LTRIM(RTRIM([value])) AS [Series]
FROM
string_split(@Series,'|')
WHERE
[value] <> ''
END;
-- Return the result set of desired [DocumentSeriesId]
-- In the real use case, DISTINCT is not used and more columns are returned.
-- However, to illustrate the issue at hand, return only the [DocumentSeriesId] as this is what we are filtering off.
SELECT DISTINCT
D1.[DocumentSeriesId]
FROM
[dbo].[Document] D1
WHERE
D1.[DocumentSeriesId] IN (SELECT SS.[SeriesId] FROM @SeriesSplit SS)
Run Code Online (Sandbox Code Playgroud)
根据需要输出两行。执行计划 (PasteThePlan)显示它执行 aDistinct Sort以过滤适当的行。
如果我将WHERE子句更改为 uses EXISTS,即使我设置只返回两个结果,我也会收到所有四种可能的结果。
-- Specify the Document Series to be returned.
DECLARE @Series varchar(14) = 'S1|S2'
-- Split the user input and insert it into a table variable.
DECLARE @SeriesSplit table ([SeriesId] tinyint, [Series] varchar(4))
BEGIN
INSERT INTO @SeriesSplit ([SeriesId], [Series])
SELECT
CASE [value] WHEN 'S1' THEN 1 WHEN 'S2' THEN 2 WHEN 'S3' THEN 3 WHEN 'S4' THEN 4 ELSE 5 END AS [SeriesId]
,LTRIM(RTRIM([value])) AS [Series]
FROM
string_split(@Series,'|')
WHERE
[value] <> ''
END;
-- Return the result set of desired [DocumentSeriesId]
-- In the real use case, DISTINCT is not used and more columns are returned.
-- However, to illustrate the issue at hand, return only the [DocumentSeriesId] as this is what we are filtering off.
SELECT DISTINCT
D1.[DocumentSeriesId]
FROM
[dbo].[Document] D1
WHERE
EXISTS (SELECT SS.[SeriesId] FROM @SeriesSplit SS JOIN [dbo].[Document] D2 ON SS.[SeriesId] = D2.[DocumentSeriesId])
Run Code Online (Sandbox Code Playgroud)
执行计划 (PasteThePlan)与这个小改动有很大不同。它折腾了警告,指出是No Join Predicate在中Left Semi Join上游。我的假设WHERE EXISTS ...将隐含地满足这个论点,就像它对WHERE [DocumentSeriesId] IN .... 但是,考虑到Left Semi Join当第二个输入中有匹配行时运算符从初始输入返回每一行,并且由于No Join Predicate存在警告,查询优化器假定每一行都是匹配行。因此,所有后续批处理操作都针对表中的所有 2048 行运行。
我可以研究什么来更好地解释执行计划所描述的内容,以便我了解如何正确解决问题?
或者,EXISTS在基于静态输入过滤结果集时,我是否只是错过了运算符的目的?
您没有正确重写查询。将INsubqquery转换为EXISTS一个时,您的EXISTS子查询应该是相关的。但是您Document在子查询中再次定义了该表,使其不相关。
您的查询基本上说
给我所有数据
Documentsif@SeriesSplit至少有一行 - 如果@SeriesSplit为空则没有任何数据。
它应该是:
SELECT DISTINCT
D1.[DocumentSeriesId]
FROM
[dbo].[Document] D1
WHERE
EXISTS (SELECT 1 FROM @SeriesSplit SS
WHERE SS.[SeriesId] = D1.[DocumentSeriesId]) ;
Run Code Online (Sandbox Code Playgroud)