Monday, January 7, 2008

SET NOCOUNT ON – A simple Performance Booster

How many of us using the SET NOCOUNT ON statement in our stored procedures? This simple one line code do a lot of differences in performance (in terms of Network Traffic which is one the performance related issue) of SQL Server.

What will this do is simply cut off the number of rows affected by the stored procedures’ Select / Insert / Delete/ Update statements. According to BOOKS ONLINE, When SET NOCOUNT is ON, the count (indicating the number of rows affected by a Transact-SQL statement) is not returned. When SET NOCOUNT is OFF, the count is returned. SET NOCOUNT ON eliminates the sending of DONE_IN_PROC messages to the client for each statement in a stored procedure.

By default value of SET NOCOUNT is OFF.

If your application really wants the number of rows affected, then you can use @@RowCount variable which is very handy.

On reading this BOL, a question is raised on my mind, that what happened if I set this on, to SQLDATAREADER’s RecordsAffected Property ? Is there any link between this SET NCOUNT? But it is not having any link. Because the remark section of the SqlDataReader.::.RecordsAffected Property in MSDN (http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.recordsaffected.aspx) is saying that The RecordsAffected property is not set until all rows are read and you close the SqlDataReader.

So to reduce network traffic, you can add this simple line at the starting of every stored procedure. For further digging read the followings.

http://www.sqlmag.com/Article/ArticleID/22093/sql_server_22093.html

http://weblogs.asp.net/jgalloway/archive/2006/08/30/How-NOCOUNT-affects-ADO.NET.aspx

http://www.mssqltips.com/tip.asp?tip=1226

3 comments:

Unknown said...

Nice dear....thanks a lot for updating us...

Buy Cialis said...

SQL Server is one of the best things in the computer, I think that SQL is so good because Clauses, which are constituent components of statements and queries.

Anonymous said...

A razo que alm das vantagens efeito rpido e frescas para tu dieta.
Estos son saludables y contienen mucha fibra, ayudando as tambin
a frenar el consumo de caloras. dieta Cetogenica En
lo personal me gusta mucho esta dieta, tiene practicamente todos patrocinado por Vedette, donde encuentras la belleza que buscas para tu cuerpo.



Un ejemplo de una estafa de dieta es la "pastillas de bloqueo de grasa y carbohidratos." Esta oferta de
dieta se supone que o rebozados pueden obstruir tus vasos
sanguneos y causar ataques al corazn. dietas para adelgazar Tu cuerpo necesita
alimentos saludables para tener la energa suficiente para hacer ejercicios, debes comer tres comidas balanceadas al seguir una dieta saludable?