我经常阅读何时必须检查行是否存在的情况,应该始终使用EXISTS而不是COUNT来完成。
但是,在最近的几种情况下,我测量了使用count时的性能提升。
模式如下:
LEFT JOIN (
SELECT
someID
, COUNT(*)
FROM someTable
GROUP BY someID
) AS Alias ON (
Alias.someID = mainTable.ID
)
我不熟悉SQL Server内部“发生什么”的方法,因此我想知道EXISTS是否存在未预料到的缺陷,该缺陷对我所做的测量完全有意义(EXISTS是RBAR吗?!)。
您对此现象有一些解释吗?
编辑:
这是您可以运行的完整脚本:
SET NOCOUNT ON
SET STATISTICS IO OFF
DECLARE @tmp1 TABLE (
ID INT UNIQUE
)
DECLARE @tmp2 TABLE (
ID INT
, X INT IDENTITY
, UNIQUE (ID, X)
)
; WITH T(n) AS (
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM master.dbo.spt_values AS S
)
, tally(n) AS (
SELECT
T2.n * 100 + T1.n
FROM T AS T1
CROSS JOIN T AS T2
WHERE T1.n <= 100
AND T2.n <= 100
)
INSERT @tmp1
SELECT n
FROM tally AS T1
WHERE n < 10000
; WITH T(n) AS (
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM master.dbo.spt_values AS S
)
, tally(n) AS (
SELECT
T2.n * 100 + T1.n
FROM T AS T1
CROSS JOIN T AS T2
WHERE T1.n <= 100
AND T2.n <= 100
)
INSERT @tmp2
SELECT T1.n
FROM tally AS T1
CROSS JOIN T AS T2
WHERE T1.n < 10000
AND T1.n % 3 <> 0
AND T2.n < 1 + T1.n % 15
PRINT '
COUNT Version:
'
WAITFOR DELAY '00:00:01'
SET STATISTICS IO ON
SET STATISTICS TIME ON
SELECT
T1.ID
, CASE WHEN n > 0 THEN 1 ELSE 0 END AS DoesExist
FROM @tmp1 AS T1
LEFT JOIN (
SELECT
T2.ID
, COUNT(*) AS n
FROM @tmp2 AS T2
GROUP BY T2.ID
) AS T2 ON (
T2.ID = T1.ID
)
WHERE T1.ID BETWEEN 5000 AND 7000
OPTION (RECOMPILE) -- Required since table are filled within the same scope
SET STATISTICS TIME OFF
PRINT '
EXISTS Version:'
WAITFOR DELAY '00:00:01'
SET STATISTICS TIME ON
SELECT
T1.ID
, CASE WHEN EXISTS (
SELECT 1
FROM @tmp2 AS T2
WHERE T2.ID = T1.ID
) THEN 1 ELSE 0 END AS DoesExist
FROM @tmp1 AS T1
WHERE T1.ID BETWEEN 5000 AND 7000
OPTION (RECOMPILE) -- Required since table are filled within the same scope
SET STATISTICS TIME OFF
在SQL Server 2008R2(七个64位)上,我得到了这个结果
COUNT
版:
表'#455F344D'。扫描计数1,逻辑读8,物理读0,预读0,lob逻辑读0,lob物理读0,lob预读0。
表'#492FC531'。扫描计数1,逻辑读30,物理读0,预读0,lob逻辑读0,lob物理读0,lob预读0。SQL Server执行时间:
CPU时间= 0毫秒,经过的时间= 81毫秒。
EXISTS
版:
表'#492FC531'。扫描计数1,逻辑读96,物理读0,预读0,lob逻辑读0,lob物理读0,lob预读0。
表'#455F344D'。扫描计数1,逻辑读8,物理读0,预读0,lob逻辑读0,lob物理读0,lob预读0。SQL Server执行时间:
CPU时间= 0毫秒,经过的时间= 76毫秒。