2011-03-31 86 views
1

当我在SQL Server Profiler中测量Writes和RowCounts时,发现报告Writes = 26035但RowCounts为0的语句(删除)。这怎么可能?很显然,该语句是在行正在写入之后删除行,为什么这些行都不在rowcount列中?SQL Server Profiler写入vs RowCounts

+0

我想重现这一点,但只是不能得到它的工作。你可以发布删除语句的代码吗? – 2011-03-31 12:31:56

+0

对不起,我没有权限发布确切的代码,但这里有一个通用版本: DELETE table1 FROM table1,table2 WHERE table1.account = table2.account – tuseau 2011-03-31 12:36:36

+1

好吧,我想我已经想通了 - delete语句没有任何删除,但Profiler正在显示写入,因为SQL Server在后台创建临时表,因为它正在处理的数据集的大小。 – tuseau 2011-03-31 12:41:12

回答

2

因为rowcount指向返回给客户端的行数。除非您使用 OUTPUT子句,否则删除操作不会返回任何行。

更新:==>我站得更正测试显示,当删除没有返回行时,也会设置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 

运行在2个表DELETE语句,用5万条记录,其中删除任何记录给我下面的查询计划::

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) 

在两个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])) 

所以哈希表导致写入的前提仍无定论。

+0

是的,我的sproc有一堆其他删除,都显示返回适当的rowcount,所以它肯定应该返回一个rowcount。 – tuseau 2011-03-31 12:32:41