不存在与不存在:效率
Not Exists vs Not In: efficiency
我一直假设 not exists 是解决问题的方法,而不是使用 not in 条件。然而,我对我一直在使用的查询进行了比较,我注意到 Not In 条件的执行实际上看起来更快。任何对为什么会出现这种情况的见解,或者如果我到目前为止只是做了一个可怕的假设,将不胜感激!
查询 1:
SELECT DISTINCT
a.SFAccountID, a.SLXID, a.Name FROM [dbo].[Salesforce_Accounts] a WITH(NOLOCK)
JOIN _SLX_AccountChannel b WITH(NOLOCK)
ON a.SLXID = b.ACCOUNTID
JOIN [dbo].[Salesforce_Contacts] c WITH(NOLOCK)
ON a.SFAccountID = c.SFAccountID
WHERE b.STATUS IN ('Active','Customer', 'Current')
AND c.Primary__C = 0
AND NOT EXISTS
(
SELECT 1 FROM [dbo].[Salesforce_Contacts] c2 WITH(NOLOCK)
WHERE a.SFAccountID = c2.SFAccountID
AND c2.Primary__c = 1
);
查询 2:
SELECT
DISTINCT
a.SFAccountID FROM [dbo].[Salesforce_Accounts] a WITH(NOLOCK)
JOIN _SLX_AccountChannel b WITH(NOLOCK)
ON a.SLXID = b.ACCOUNTID
JOIN [dbo].[Salesforce_Contacts] c WITH(NOLOCK)
ON a.SFAccountID = c.SFAccountID
WHERE b.STATUS IN ('Active','Customer', 'Current')
AND c.Primary__C = 0
AND a.SFAccountID NOT IN (SELECT SFAccountID FROM [dbo].[Salesforce_Contacts] WHERE Primary__c = 1 AND SFAccountID IS NOT NULL);
查询 1 的实际执行计划:
查询 2 的实际执行计划:
TIME/IO 统计数据:
查询 #1(使用不存在):
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server parse and compile time:
CPU time = 532 ms, elapsed time = 533 ms.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Salesforce_Contacts'. Scan count 2, logical reads 3078, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'INFORMATION'. Scan count 1, logical reads 691, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ACCOUNT'. Scan count 4, logical reads 567, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Salesforce_Accounts'. Scan count 1, logical reads 680, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 250 ms, elapsed time = 271 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
查询 #2(使用 Not In):
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server parse and compile time:
CPU time = 500 ms, elapsed time = 500 ms.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Salesforce_Contacts'. Scan count 2, logical reads 3079, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'INFORMATION'. Scan count 1, logical reads 691, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ACCOUNT'. Scan count 4, logical reads 567, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Salesforce_Accounts'. Scan count 1, logical reads 680, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 157 ms, elapsed time = 166 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
尝试
SELECT DISTINCT a.SFAccountID, a.SLXID, a.Name
FROM [dbo].[Salesforce_Accounts] a WITH(NOLOCK)
JOIN _SLX_AccountChannel b WITH(NOLOCK)
ON a.SLXID = b.ACCOUNTID
AND b.STATUS IN ('Active','Customer', 'Current')
JOIN [dbo].[Salesforce_Contacts] c WITH(NOLOCK)
ON a.SFAccountID = c.SFAccountID
AND c.Primary__C = 0
LEFT JOIN [dbo].[Salesforce_Contacts] c2 WITH(NOLOCK)
on c2.SFAccountID = a.SFAccountID
AND c2.Primary__c = 1
WHERE c2.SFAccountID is null
据我了解,a not 的工作方式与两个嵌套 for 指令的工作方式相同。
所以,假设您有两个 table:table(1000 条记录)和塔布拉鼓(2000 条记录),
select * from table where table.field not in (select field from tabla)
就像在做
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 2000; j++) {
}
}
即1000*2000 = 200万次操作
与 tabla.field 的左连接是 null 技巧,据我所知,它只进行了 2000 次操作
使用左连接。
这是假设您正在尝试查找没有主要联系人并且只能有一个主要联系人的帐户
SELECT a.SFAccountID, a.SLXID, a.Name
FROM [dbo].[Salesforce_Accounts] a
LEFT JOIN [dbo].[Salesforce_Contacts] c ON a.SFAccountID = c.SFAccountID AND c.Primary__C = 1
WHERE
EXISTS (SELECT *
FROM SLX_AccountChannel b
WHERE b.ACCOUNTID = a.SLXID
AND b.STATUS IN ( 'Active', 'Customer', 'Current' ))
AND c.SFContactID IS NULL
如果您想要有联系人但没有主要联系人的帐户,您可以使用
SELECT
a.SFAccountID ,
a.SLXID ,
a.Name
FROM
[dbo].[Salesforce_Accounts] a
WHERE
a.SFAccountID IN (SELECT SFAccountID
FROM [Salesforce_Contacts]
GROUP BY SFAccountID
HAVING SUM(CAST(Primary__c AS INT) = 0))
AND a.SLXID IN (SELECT ACCOUNTID
FROM _SLX_AccountChannel
WHERE [STATUS] IN ( 'Active', 'Customer', 'Current' ))
我认为缺少索引会导致 EXISTS()
和 IN
操作不同。
虽然问题不要求更好的查询,但对我来说我会尽量避免这样的Distinct
SELECT
a.SFAccountID, a.SLXID, a.Name
FROM
[dbo].[Salesforce_Accounts] a WITH(NOLOCK)
CROSS APPLY
(
SELECT SFAccountID
FROM [dbo].[Salesforce_Contacts] WITH(NOLOCK)
WHERE SFAccountID = a.SFAccountID
GROUP BY SFAccountID
HAVING MAX(Primary__C + 0) = 0 -- Assume Primary__C is a bit value
) b
WHERE
-- Actually it is the filtering condition for account channel
EXISTS
(
SELECT * FROM _SLX_AccountChannel WITH(NOLOCK)
WHERE ACCOUNTID = a.SLXID AND STATUS IN ('Active','Customer', 'Current')
)
问题是:"why NOT IN
appears to be faster than NOT EXISTS
"。
我的回答是:只是看起来更快,其实是一样的。 (在这种情况下)
您是否实际测量了两个查询的时间并确认存在差异?
或者您刚刚查看了执行计划?
据我了解,您在屏幕截图上看到的查询成本(53% 对 47%)是:
- 估计查询成本,即使计划是实际的;
- 是查询成本,不是时间,由CPU和IO"costs".
组合而成
在这种特殊情况下,查询优化器似乎为两个查询生成了几乎相同的计划。计划中某些运算符的估计行数很可能(略有)不同,但实际性能是相同的,因为计划形状相同。如果估计的行数不同,将导致您看到不同的估计查询成本。
要查看计划中的差异(如果有的话),我会使用像 SQL Sentry Plan Explorer 这样的工具。它显示了更多详细信息,您可以更轻松地比较查询的各个方面。
重写查询以使其更快是另一个问题,我不会在这里尝试回答。
您可以多次不用 hitting/joining Salesforce_Contacts
。这样更紧凑也更快:
SELECT a.SFAccountID, a.SLXID, a.Name
FROM [dbo].[Salesforce_Accounts] a WITH(NOLOCK)
JOIN _SLX_AccountChannel b WITH(NOLOCK)
ON a.SLXID = b.ACCOUNTID
JOIN [dbo].[Salesforce_Contacts] c WITH(NOLOCK)
ON a.SFAccountID = c.SFAccountID
WHERE b.STATUS IN ('Active','Customer', 'Current')
GROUP BY a.SFAccountID, a.SLXID, a.Name
HAVING MAX(c.Primary__C) = 0
IN
和 EXISTS
之间的差异可以忽略不计。
我一直假设 not exists 是解决问题的方法,而不是使用 not in 条件。然而,我对我一直在使用的查询进行了比较,我注意到 Not In 条件的执行实际上看起来更快。任何对为什么会出现这种情况的见解,或者如果我到目前为止只是做了一个可怕的假设,将不胜感激!
查询 1:
SELECT DISTINCT
a.SFAccountID, a.SLXID, a.Name FROM [dbo].[Salesforce_Accounts] a WITH(NOLOCK)
JOIN _SLX_AccountChannel b WITH(NOLOCK)
ON a.SLXID = b.ACCOUNTID
JOIN [dbo].[Salesforce_Contacts] c WITH(NOLOCK)
ON a.SFAccountID = c.SFAccountID
WHERE b.STATUS IN ('Active','Customer', 'Current')
AND c.Primary__C = 0
AND NOT EXISTS
(
SELECT 1 FROM [dbo].[Salesforce_Contacts] c2 WITH(NOLOCK)
WHERE a.SFAccountID = c2.SFAccountID
AND c2.Primary__c = 1
);
查询 2:
SELECT
DISTINCT
a.SFAccountID FROM [dbo].[Salesforce_Accounts] a WITH(NOLOCK)
JOIN _SLX_AccountChannel b WITH(NOLOCK)
ON a.SLXID = b.ACCOUNTID
JOIN [dbo].[Salesforce_Contacts] c WITH(NOLOCK)
ON a.SFAccountID = c.SFAccountID
WHERE b.STATUS IN ('Active','Customer', 'Current')
AND c.Primary__C = 0
AND a.SFAccountID NOT IN (SELECT SFAccountID FROM [dbo].[Salesforce_Contacts] WHERE Primary__c = 1 AND SFAccountID IS NOT NULL);
查询 1 的实际执行计划:
查询 2 的实际执行计划:
TIME/IO 统计数据:
查询 #1(使用不存在):
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server parse and compile time:
CPU time = 532 ms, elapsed time = 533 ms.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Salesforce_Contacts'. Scan count 2, logical reads 3078, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'INFORMATION'. Scan count 1, logical reads 691, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ACCOUNT'. Scan count 4, logical reads 567, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Salesforce_Accounts'. Scan count 1, logical reads 680, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 250 ms, elapsed time = 271 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
查询 #2(使用 Not In):
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server parse and compile time:
CPU time = 500 ms, elapsed time = 500 ms.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Salesforce_Contacts'. Scan count 2, logical reads 3079, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'INFORMATION'. Scan count 1, logical reads 691, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ACCOUNT'. Scan count 4, logical reads 567, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Salesforce_Accounts'. Scan count 1, logical reads 680, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 157 ms, elapsed time = 166 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
尝试
SELECT DISTINCT a.SFAccountID, a.SLXID, a.Name
FROM [dbo].[Salesforce_Accounts] a WITH(NOLOCK)
JOIN _SLX_AccountChannel b WITH(NOLOCK)
ON a.SLXID = b.ACCOUNTID
AND b.STATUS IN ('Active','Customer', 'Current')
JOIN [dbo].[Salesforce_Contacts] c WITH(NOLOCK)
ON a.SFAccountID = c.SFAccountID
AND c.Primary__C = 0
LEFT JOIN [dbo].[Salesforce_Contacts] c2 WITH(NOLOCK)
on c2.SFAccountID = a.SFAccountID
AND c2.Primary__c = 1
WHERE c2.SFAccountID is null
据我了解,a not 的工作方式与两个嵌套 for 指令的工作方式相同。
所以,假设您有两个 table:table(1000 条记录)和塔布拉鼓(2000 条记录),
select * from table where table.field not in (select field from tabla)
就像在做
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 2000; j++) {
}
}
即1000*2000 = 200万次操作
与 tabla.field 的左连接是 null 技巧,据我所知,它只进行了 2000 次操作
使用左连接。
这是假设您正在尝试查找没有主要联系人并且只能有一个主要联系人的帐户
SELECT a.SFAccountID, a.SLXID, a.Name
FROM [dbo].[Salesforce_Accounts] a
LEFT JOIN [dbo].[Salesforce_Contacts] c ON a.SFAccountID = c.SFAccountID AND c.Primary__C = 1
WHERE
EXISTS (SELECT *
FROM SLX_AccountChannel b
WHERE b.ACCOUNTID = a.SLXID
AND b.STATUS IN ( 'Active', 'Customer', 'Current' ))
AND c.SFContactID IS NULL
如果您想要有联系人但没有主要联系人的帐户,您可以使用
SELECT
a.SFAccountID ,
a.SLXID ,
a.Name
FROM
[dbo].[Salesforce_Accounts] a
WHERE
a.SFAccountID IN (SELECT SFAccountID
FROM [Salesforce_Contacts]
GROUP BY SFAccountID
HAVING SUM(CAST(Primary__c AS INT) = 0))
AND a.SLXID IN (SELECT ACCOUNTID
FROM _SLX_AccountChannel
WHERE [STATUS] IN ( 'Active', 'Customer', 'Current' ))
我认为缺少索引会导致 EXISTS()
和 IN
操作不同。
虽然问题不要求更好的查询,但对我来说我会尽量避免这样的Distinct
SELECT
a.SFAccountID, a.SLXID, a.Name
FROM
[dbo].[Salesforce_Accounts] a WITH(NOLOCK)
CROSS APPLY
(
SELECT SFAccountID
FROM [dbo].[Salesforce_Contacts] WITH(NOLOCK)
WHERE SFAccountID = a.SFAccountID
GROUP BY SFAccountID
HAVING MAX(Primary__C + 0) = 0 -- Assume Primary__C is a bit value
) b
WHERE
-- Actually it is the filtering condition for account channel
EXISTS
(
SELECT * FROM _SLX_AccountChannel WITH(NOLOCK)
WHERE ACCOUNTID = a.SLXID AND STATUS IN ('Active','Customer', 'Current')
)
问题是:"why NOT IN
appears to be faster than NOT EXISTS
"。
我的回答是:只是看起来更快,其实是一样的。 (在这种情况下)
您是否实际测量了两个查询的时间并确认存在差异?
或者您刚刚查看了执行计划?
据我了解,您在屏幕截图上看到的查询成本(53% 对 47%)是:
- 估计查询成本,即使计划是实际的;
- 是查询成本,不是时间,由CPU和IO"costs". 组合而成
在这种特殊情况下,查询优化器似乎为两个查询生成了几乎相同的计划。计划中某些运算符的估计行数很可能(略有)不同,但实际性能是相同的,因为计划形状相同。如果估计的行数不同,将导致您看到不同的估计查询成本。
要查看计划中的差异(如果有的话),我会使用像 SQL Sentry Plan Explorer 这样的工具。它显示了更多详细信息,您可以更轻松地比较查询的各个方面。
重写查询以使其更快是另一个问题,我不会在这里尝试回答。
您可以多次不用 hitting/joining Salesforce_Contacts
。这样更紧凑也更快:
SELECT a.SFAccountID, a.SLXID, a.Name
FROM [dbo].[Salesforce_Accounts] a WITH(NOLOCK)
JOIN _SLX_AccountChannel b WITH(NOLOCK)
ON a.SLXID = b.ACCOUNTID
JOIN [dbo].[Salesforce_Contacts] c WITH(NOLOCK)
ON a.SFAccountID = c.SFAccountID
WHERE b.STATUS IN ('Active','Customer', 'Current')
GROUP BY a.SFAccountID, a.SLXID, a.Name
HAVING MAX(c.Primary__C) = 0
IN
和 EXISTS
之间的差异可以忽略不计。