如您所见,EXISTS和COUNT(*)>0的IO统计信息相同。我认为EXISTS会更快,执行更少的读取。我是不是丢了什么东西?
SQL Server
按块读取数据。
如果您要查找的记录恰好位于最后一个块中(或根本找不到),则
EXISTS
仍然需要扫描所有的区块。
定义主键会有什么不同吗?
对。
PRIMARY KEY
,
存在
和
COUNT(*)
总是相同的(单索引搜索)。
存在
计数(*)
更新:
对不起,我刚才错了。
SQL服务器
优化
COUNT(*) > 0
到
存在
DBCC FreeProcCache
CHECKPOINT
DBCC DROPCLEANBUFFERS
SET STATISTICS IO ON
DECLARE @id UNIQUEIDENTIFIER
SET @id = '7C65EBB1-1242-4084-8BB4-3CC92CA2BE51'
IF (
SELECT COUNT(*)
FROM tablewithhugedata
WHERE id = @id
) > 0
BEGIN
SELECT CONVERT(BIT, 1)
END
ELSE
BEGIN
SELECT CONVERT(BIT, 0)
END
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Table 'TableWithHugeData'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 270, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
(1 row(s) affected)
还有一点不同的问题:
DBCC FreeProcCache
CHECKPOINT
DBCC DROPCLEANBUFFERS
SET STATISTICS IO ON
DECLARE @id UNIQUEIDENTIFIER
SET @id = '7C65EBB1-1242-4084-8BB4-3CC92CA2BE51'
IF EXISTS
(
SELECT 1
FROM tablewithhugedata
WHERE id = @id
)
BEGIN
SELECT CONVERT(BIT, 1)
END
ELSE
BEGIN
SELECT CONVERT(BIT, 0)
END
DBCC FreeProcCache
CHECKPOINT
DBCC DROPCLEANBUFFERS
SET STATISTICS IO ON
DECLARE @id UNIQUEIDENTIFIER
SET @id = '7C65EBB1-1242-4084-8BB4-3CC92CA2BE51'
IF (
SELECT COUNT(*)
FROM tablewithhugedata
WHERE id = @id
) BETWEEN 1 AND 2
BEGIN
SELECT CONVERT(BIT, 1)
END
ELSE
BEGIN
SELECT CONVERT(BIT, 0)
END
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Table 'TableWithHugeData'. Scan count 2, logical reads 266, physical reads 6, read-ahead reads 270, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
(1 row(s) affected)
计数(*)
所以它读所有的方块(
266
逻辑读取与。
1
对于
EXISTS / COUNT(*) > 0
).