關於Set Nocount ON的效能 |c#調用預存程序的傳回值總是-1

來源:互聯網
上載者:User

標籤:

原文地址:http://www.tuicool.com/articles/qe6BZbR

也許因為它太過於簡單,自己一直沒能好好關注這個語句,只記得"只是"提高點效能而已.有時會在預存程序中寫上幾句,有時也會懶得去敲這幾個字母.但是.他們SET NOCOUNT ON 和SET NOCOUNT OFF 之間到底有多大的區別嗎?前天一時好奇.終於想弄清楚他們之間效能有多大區別.在google一遍,找了幾篇文章.我們可以得出一些結論.

How NOCOUNT affects ADO.NET (NOCOUNT對ADO.NET影響多大)by Jon Galloway

在文章的評論有一段測試的代碼,大家可以複製到查詢分析器進行測試.基本上,SET NOCOUNT ON比OFF更快點(但我測試的結果不是很理想,基本上二者相差不是太大,更誇張是文章下面的評論:

I want to share my experience with NOCOUNT.

A stored procedure who joins a few tables with more than 100.000 rows is very slow if you run it with ADO.NET and with option NOCOUNT ON. By setting NOCOUNT OFF the same procedure will be 10 times faster.

There is no difference if you execute this procedure from the Management Studio with NOCOUNT OFF or ON

MY GOD!

 Seeing Is Believing

基本上他們的結論是:使用NOCOUNT能夠減少網路的傳輸.當我們SET NOCOUNT ON時執行的預存程序每執行sql語句(像

SELECT, INSERT, UPDATE, DELETE)時會忽略向用戶端發送

DONE_IN_PROC訊息.

如果我們判斷Update更新資料是否成功時,最簡便的方法就是ExecuteNonQuery()>0.在使用SET NOCOUNT ON時, ExecuteNonQuery總是返回-1 . 一個很好的解決方案就是使用out parameter方式來輸出是否成功 it uses the rows affected result to determine if the update succeeded .

另外有一篇 T-SQL 編碼通訊協定 是一篇不錯的文章.非常值得一讀.

 

原文地址:http://www.cnblogs.com/cnzc/archive/2007/09/01/878434.html

--------------------------------------------------------------------------

 

public virtual int GetMenuByRID(Nullable<int> rID)

{

var rIDParameter = rID.HasValue ?

new ObjectParameter("RID", rID) :

new ObjectParameter("RID", typeof(int));

 

return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("GetMenuByRID", rIDParameter);

}

 

ObjectContext.ExecuteFunction 這個返回的是影響的行數 

關於Set Nocount ON的效能 |c#調用預存程序的傳回值總是-1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.