维生素C.net
Talents come from diligence, and knowledge is gained by accumulation 天才源于勤奋,知识源于积累。
难忘的1654天
博客园  首页  新随笔  新文章  联系  管理  订阅 订阅
随笔- 220  文章- 1  评论- 1643 
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 阅读(1585) 评论(0)  编辑 收藏 所属分类: ASP.NET
社区  新闻  新用户注册  刷新评论列表  

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2006-09-05 16:45 编辑过
 
历史上的今天:
  • 2005-09-05 Vista中的UAP

所属分类的其他文章:
· 为LINQ提速的i4o和增强功能的SLINQ
· 结构体,值类型和接口
· ASP.NET崩溃 - SiteMap里的疯狂循环(概译)
· Inbox.com使用的asp.net和ajax技术
· NOCOUNT如何影响ADO.NET(SET NOCOUNT ON的性能问题)
· 30hrs Wire [1]
· 利用数学方法来大大降低一个逻辑判断实现的难度的例子
· 在配置使用Membership或其他的Providers的ASP.NET2.0时一定要设置applicationName属性
· 在Atlas中实现检测postback progress的状态的方法
· 【翻译】Atlas Documents : UpdatePanel Class
最新IT新闻:
· Windows Live视频邮件9月9日开始测试
· Intel首次公开展示Nehalem架构迅驰3平台
· Pogo浏览器
· 洪磊口述:番茄花园如何捆绑流氓软件月入十万
· 2008年8月21日IT博客精选:盖茨复出?
博客园新闻频道  博客园首页  社区
 



公告

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

<2006年9月>
日一二三四五六
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

与我联系

  • 发短消息

搜索

 

常用链接

  • 我的随笔
  • 我的空间
  • 我的短信
  • 我的评论
  • 更多链接
  • 我的参与
  • 我的新闻
  • 最新评论
  • 我的标签

留言簿(168)

  • 给我留言
  • 查看留言

我参与的团队

  • 北京.NET俱乐部(1/1446)
  • 烟台.NET俱乐部(0/47)
  • ASP.NET AJAX (Atlas)学习(0/1340)
  • MVP(微软最有价值专家)团队(1/540)
  • 博客园培训团队(0/110)
  • Silverlight学习与研究(0/282)
  • CLR基础研究团队(0/374)

随笔分类(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
  • 小涛
  • 小新
  • 肖老师
  • 旋哥

积分与排名

  • 积分 - 393425
  • 排名 - 53

最新评论

  • 1. re: .NET Beginner Training Step by Step开始启动
  • 申请加入
  • --Winston
  • 2. re: .NET Beginner Training Step by Step开始启动
  • 申请加入
  • --Birdshover
  • 3. re: .NET Beginner Training Step by Step开始启动
  • 喜欢没有理由!
    申请加入
  • --倔强
  • 4. re: .NET Beginner Training Step by Step开始启动
  • 申请加入
  • --清道夫-WPF
  • 5. re: .NET Beginner Training Step by Step开始启动
  • 申请加入,向大家学习
  • --ten.psa

阅读排行榜

  • 1. 英文名字及含义(25073)
  • 2. SQL Server 2005 Remote Access(14891)
  • 3. Visual Studio 2005 Team Edition和SQL Server 2005的下载(14282)
  • 4. Windows Installer 3.1(11433)
  • 5. Visual Studio 2005 Professional Released(10925)

评论排行榜

  • 1. .NET Beginner Training Step by Step开始启动(317)
  • 2. Windows Live Messenger 8.0 Beta 的邀请(100)
  • 3. 加入[ 下一代网络web技术(Next Generation Web Application)团队Blog ](88)
  • 4. 博客园新手.net技术培训活动(55)
  • 5. 为什么在vista上做开发?(54)
Copyright ©2008 维生素C.NET