当我在SQL Server Profiler中测量Writes和RowCounts时,我发现一个语句(删除)报告Writes = 26035,但RowCounts为0.这怎么可能?显然,该语句是在写行时删除行,那么为什么这些行都不计入rowcount列?
答案 0 :(得分:2)
因为rowcount指向返回给客户端的行数。除非使用 OUTPUT
子句,否则删除不会返回任何行。
更新:==>我站得更正测试显示当delete没有返回行时也会设置rowcount。
证明:
SELECT *
FROM Accounts
WHERE Category= 'COA'
AND Code between 1500 and 2000
-- 18 Reads, 0 Writes, 51 RowCount
DELETE FROM Accounts
WHERE Category = 'COA'
AND Code between 1500 and 2000
-- 22 Reads, 1 Writes, 51 RowCount
DELETE FROM Accounts
OUTPUT DELETED.*
WHERE Category = 'COA'
AND Code between 2000 and 4000
-- 24 Reads, 3 Writes, 103 RowCount
在包含500万条记录的2个表上运行删除语句,这些记录不会删除任何记录,而是提供以下查询计划::
delete clust from clust, heap where clust.Key= heap.Key
-- 19854 Reads, 0 Writes, 0 RowCount
|--Clustered Index Delete(OBJECT:([dbo].[clust].[idx_clust]), OBJECT:([dbo].[clust].[idx2_clust])) |--Top(ROWCOUNT est 0) |--Parallelism(Gather Streams) |--Hash Match(Right Semi Join, HASH:([dbo].[heap].[Key])=([dbo].[clust].[Key])) |--Bitmap(HASH:([dbo].[heap].[Key]), DEFINE:([Bitmap1012])) | |--Parallelism(Repartition Streams, Hash Partitioning, PARTITION COLUMNS:([dbo].[heap].[Key])) | |--Stream Aggregate(GROUP BY:([dbo].[heap].[Key])) | |--Index Scan(OBJECT:([dbo].[heap].[idx_heap]), ORDERED FORWARD) |--Parallelism(Repartition Streams, Hash Partitioning, PARTITION COLUMNS:([dbo].[clust].[Key])) |--Index Scan(OBJECT:([dbo].[clust].[idx2_clust]), WHERE:(PROBE([Bitmap1012],[dbo].[clust].[Key],N'[IN ROW]')) ORDERED FORWARD)
在2个小表上运行相同的查询,每个表有10行,结果如下:
delete smallclust from smallclust, smallheap where smallclust.srp_key = smallheap.common_key
-- 45 Reads, 0 Writes, 0 RowCount
|--Table Delete(OBJECT:([dbo].[smallclust])) |--Top(ROWCOUNT est 0) |--Nested Loops(Left Semi Join, WHERE:([dbo].[smallheap].[Key]=[dbo].[smallclust].[Key])) |--Table Scan(OBJECT:([dbo].[smallclust])) |--Table Scan(OBJECT:([dbo].[smallheap]))
因此,导致写入的哈希表的假设仍然不确定。