维生素C.net
Talents come from diligence, and knowledge is gained by accumulation 天才源于勤奋,知识源于积累。
难忘的1654天
博客园  首页  新随笔  新文章  联系  管理  订阅 订阅
随笔- 220  文章- 1  评论- 1659 
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%

如果您也亲自测试一下可以很明显的发现:结果对于证明起到的作用很小。

绿色通道:好文要顶关注我收藏该文与我联系
posted on 2006-09-05 16:19 维生素C.NET 阅读(1807) 评论(0) 编辑 收藏
注册用户登录后才能发表评论,请 登录 或 注册,返回博客园首页。
首页博问闪存新闻园子招聘知识库
最新IT新闻:
· 春节后礼品回收iPhone成新宠燕窝被冷落
· 分析称苹果近1000亿现金储备最佳用途是派息
· 扎克伯格11件蠢事:曾同意将Facebook卖给雅虎
· 最想要的Entity Framework功能
· 专访Jeffrey Richter:Windows 8是微软的重中之重
» 更多新闻...
最新知识库文章:
· 高级编程语言的发展历程
· 如何学习一门新的编程语言?
· 学习不同编程语言的重要性
· 为什么我喜欢富于表达性的编程语言
· 计算机专业的女生为什么要学编程
» 更多知识库文章...

China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务



公告

view my mvp profile 看看有多少人来访问我的Blog了!
hotmail

昵称:维生素C.NET
园龄:7年8个月
粉丝:9
关注:0
<2006年9月>
日一二三四五六
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

搜索

 
 

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论
  • 我的标签

随笔分类(148)

  • ASP.NET(26)
  • Code Warehouse(20)
  • IronRuby,DLR(2)
  • LINQ(3)
  • Reading(3)
  • Training@cnblogs(23)
  • Ubuntu(4)
  • Windows Live(6)
  • Windows Mobile(7)
  • XHTML & Web Standard(54)

随笔档案(220)

  • 2008年3月 (2)
  • 2008年1月 (3)
  • 2007年12月 (3)
  • 2007年9月 (1)
  • 2007年8月 (2)
  • 2007年7月 (3)
  • 2007年6月 (3)
  • 2007年3月 (4)
  • 2007年2月 (3)
  • 2007年1月 (1)
  • 2006年12月 (1)
  • 2006年11月 (8)
  • 2006年10月 (6)
  • 2006年9月 (11)
  • 2006年8月 (5)
  • 2006年7月 (4)
  • 2006年6月 (1)
  • 2006年5月 (10)
  • 2006年4月 (8)
  • 2006年2月 (2)
  • 2006年1月 (1)
  • 2005年12月 (11)
  • 2005年11月 (13)
  • 2005年10月 (3)
  • 2005年9月 (1)
  • 2005年8月 (4)
  • 2005年7月 (3)
  • 2005年6月 (4)
  • 2005年4月 (5)
  • 2005年3月 (10)
  • 2005年2月 (7)
  • 2005年1月 (28)
  • 2004年12月 (15)
  • 2004年11月 (10)
  • 2004年10月 (5)
  • 2004年9月 (1)
  • 2004年6月 (13)
  • 2004年5月 (5)

文章档案(1)

  • 2005年5月 (1)

相册

  • ASPNET2tutorial
  • BlogUsing
  • My love and my friends
  • newGallery
  • 下一代网络图片

.net网站收藏

  • ASP.NET2.0 Tutorial
  • CodeBetter.com
  • F#
  • IIS.net
  • MS NewsGroup
  • NewsGroups
  • OnlyVC.org
  • VWD2005GuidedTour
  • ZDNet China软件技术专区

OSS 2007

  • Charsh
  • Kaneboy
  • Official Team Blog

Python

  • BeginnersGuide

好友的BLOG

  • DemoFox@JoyCode
  • DflyingChen
  • dudu
  • EricLee
  • hbifts
  • idior
  • Jesee Qing
  • Lion
  • Rickie
  • Samuel
  • Steph`s Website
  • 翱翔.Net
  • 陈敬熙
  • 发条木偶
  • 葛涵涛
  • 古道风
  • 寒枫天伤
  • 老猫の理想
  • 刘老师
  • 刘彦博
  • 吕震宇
  • 木野狐
  • 佘广
  • 王sir
  • 小涛
  • 小新
  • 肖老师
  • 旋哥

积分与排名

  • 积分 - 440296
  • 排名 - 140

最新评论

阅读排行榜

评论排行榜

推荐排行榜

Copyright ©2012 维生素C.NET