当我在 SQL Server Profiler 中测量 Writes 和 RowCounts 时,我发现一条语句(删除)报告 Writes = 26035,但 RowCounts 为 0。这怎么可能?显然,该语句正在删除行,因为它正在写入,那么为什么这些都没有计入 rowcount 列?
问问题
1632 次
1 回答
2
因为 rowcount 指向返回给客户端的行数。OUTPUT
除非您使用该子句,否则删除不会返回任何行。
更新:==> 我站得更正测试显示,当删除不返回行时,也会设置行数。
证明:
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
在 2 个包含 500 万条记录且不删除任何记录的表上运行删除语句给我以下查询计划:
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]))
因此,导致写入的哈希表的假设仍然没有定论。
于 2011-03-31T12:11:25.767 回答