NOCOUNT如何影响ADO.NET(SET NOCOUNT ON的性能问题)
How NOCOUNT affects ADO.NET一文中提到:
Previously, in Classic ASP and ADO, NOCOUNT was most commonly a factor if you were checking RecordsAffected on Command.Execute(). SqlDataReader does have a RecordsAffected property, but it's rarely used since it's not populated until the datareader has iterated all the rows and the datareader is closed. There are some possible implications if you're using DataAdapters to submit changes to the database, since it uses the rows affected result to determine if the update succeeded. Probably the easiest way to check for that case is to search the codebase for SqlDataAdapter and see if the Update() method is being called.
还有:
Tech notes if you're interested in tracing how RecordsAffected is set and used by pointing Reflector at System.Data:
System.Data.SqlClient.TdsParser.ProcessDone() sets RecordsAffected.
System.Data.Common.DbDataAdapter.UpdateRow() uses reader.RecordsAffected in determining whether to call AcceptChanges and ApplyToDataRow.
保险一点来说,通过作者所说的调用Update()来做判断似乎并不可靠,中间有很多步骤都有可能出问题从而影响最终的结果。同时,debug起来的也有麻烦了,嵌入sql debug后对于调试程序很有用,而如果按作者所说这个debug的功能都被削减了。又把database和application给分开调了。
评论中有人写了一个简单的测试程序,并给出了他的测试结果,他认为还是SET NOCOUNT ON快。
DECLARE @Start DATETIME, @End DATETIME, @Counter INT, @NoCountOff INT, @NoCountOn INT
CREATE TABLE #MyTable (
Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY
, FirstName VARCHAR(128) NOT NULL
, LastName VARCHAR(128) NOT NULL
)
SET NOCOUNT OFF
SET @Counter = 1
SET @Start = GETDATE()
WHILE @Counter <= 150000
BEGIN
INSERT #MyTable (FirstName, LastName) VALUES ('Scott', 'Whigham')
SET @Counter = @Counter + 1
END
SET @End = GETDATE()
SELECT @NoCountOff = DATEDIFF(ms, @Start, @End)
SET NOCOUNT ON
SET @Counter = 1
SET @Start = GETDATE()
WHILE @Counter <= 150000
BEGIN
INSERT #MyTable (FirstName, LastName) VALUES ('Scott', 'Whigham')
SET @Counter = @Counter + 1
END
SET @End = GETDATE()
SELECT @NoCountOn = DATEDIFF(ms, @Start, @End)
SELECT @NoCountOff AS 'NoCountOff', @NoCountOn AS 'NoCountOn', COUNT(*) AS NumberOfRows FROM #MyTable
DROP TABLE #MyTable
我与他测试的环境不同,在xp + SQL 2005 Ent with SP1上测试的,结果结论相反,把结果数据也share给大家:
NoCountOff | NoCountOn | xRate
7106 5893 20.58%
6156 6890 -10.61%
7093 7343 -3.41%
5780 6406 -9.77%
5563 6860 -18.91%
5610 7390 -24.09%
5810 6923 -21.38%
6296 7436 -15.33%
-- insert 1
46 0
16 0
0 0
0 0
16 0
-- insert 1000
420 393 6.88%
423 390 8.47%
966 423 128.37%
-- insert 100,000
43250 42860 0.91%
-- delete all then insert 100,000
42076 39826 5.65%
如果您也亲自测试一下可以很明显的发现:结果对于证明起到的作用很小。
总结
以上是生活随笔为你收集整理的NOCOUNT如何影响ADO.NET(SET NOCOUNT ON的性能问题)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: DataGrid 中的特殊应用
- 下一篇: asp.net学习资源列表