﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server 2008 - General  / TSQL Optimization - CASTing and CURSOR / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Mon, 20 May 2013 19:42:10 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: TSQL Optimization - CASTing and CURSOR</title><link>http://www.sqlservercentral.com/Forums/Topic1422379-391-1.aspx</link><description>[quote][b]Jay-246689 (3/20/2013)[/b][hr]Thanks for all the reply, i been focus on other things and just read your messages. So it seems it is already optimized (Casting). Yes, on the latter part they used it to do insert/update on this field and some cases use it as a condition.For the cursor here below is the scripts. This is created as scalar-valued functions, and being called multiples times in one process, &amp;gt;5millions calls. The results/returns records of each cursor called will not be more than 10 records and most of the times, 95%, it will only return one record. dbo.table holds more or less 800K records. And there are only 4 objects dependent on this function. Can you suggest any optimization on this scripts.-----------------  DECLARE @m INT  SET @m = 0    DECLARE @l INT  SET @l = 0    DECLARE l_cursor CURSOR FOR   SELECT LId  FROM [dbo].table  WHERE EH = @param1  ORDER BY LId  OPEN l_cursor  FETCH NEXT FROM l_cursor  INTO @l  WHILE @@FETCH_STATUS = 0  BEGIN			 IF @l = 0  SET @m = @m | 0x0001 		ELSE IF @l = 1  SET @m = @m | 0x0009		ELSE IF @l = 2  SET @m = @m | 0x0011                ........multiple else if				FETCH NEXT FROM l_cursor 		INTO @l  END  CLOSE l_cursor  DEALLOCATE l_cursor    return @m;--------------Thanks again.[/quote]A little hard to offer advice on how to rewrite a cursor when you don't even provide the entire code.</description><pubDate>Wed, 20 Mar 2013 23:53:56 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: TSQL Optimization - CASTing and CURSOR</title><link>http://www.sqlservercentral.com/Forums/Topic1422379-391-1.aspx</link><description>Thanks for all the reply, i been focus on other things and just read your messages. So it seems it is already optimized (Casting). Yes, on the latter part they used it to do insert/update on this field and some cases use it as a condition.For the cursor here below is the scripts. This is created as scalar-valued functions, and being called multiples times in one process, &amp;gt;5millions calls. The results/returns records of each cursor called will not be more than 10 records and most of the times, 95%, it will only return one record. dbo.table holds more or less 800K records. And there are only 4 objects dependent on this function. Can you suggest any optimization on this scripts.-----------------  DECLARE @m INT  SET @m = 0    DECLARE @l INT  SET @l = 0    DECLARE l_cursor CURSOR FOR   SELECT LId  FROM [dbo].table  WHERE EH = @param1  ORDER BY LId  OPEN l_cursor  FETCH NEXT FROM l_cursor  INTO @l  WHILE @@FETCH_STATUS = 0  BEGIN			 IF @l = 0  SET @m = @m | 0x0001 		ELSE IF @l = 1  SET @m = @m | 0x0009		ELSE IF @l = 2  SET @m = @m | 0x0011                ........multiple else if				FETCH NEXT FROM l_cursor 		INTO @l  END  CLOSE l_cursor  DEALLOCATE l_cursor    return @m;--------------Thanks again.</description><pubDate>Wed, 20 Mar 2013 23:45:01 GMT</pubDate><dc:creator>Jay-246689</dc:creator></item><item><title>RE: TSQL Optimization - CASTing and CURSOR</title><link>http://www.sqlservercentral.com/Forums/Topic1422379-391-1.aspx</link><description>[quote]Is CASTing cost an additional overhead to the query? [/quote]In this case, no.  In fact, here it might actually help ever so slightly.SQL has to determine a datatype for every column anyway -- think about it, you can't have a table with a column with an unknown datatype.  By explicitly CASTing it, you prevent SQL from having to try to determine a column datatype itself.</description><pubDate>Thu, 21 Feb 2013 10:08:56 GMT</pubDate><dc:creator>ScottPletcher</dc:creator></item><item><title>RE: TSQL Optimization - CASTing and CURSOR</title><link>http://www.sqlservercentral.com/Forums/Topic1422379-391-1.aspx</link><description>[quote][b]Jay-246689 (2/20/2013)[/b][hr]hi,Is CASTing cost an additional overhead to the query? I need to optimize our database initialization and i reviewing some of the sql scripts and found out that there are several sp using this  CAST( NULL AS VARBINARY(MAX)) in defining their column. If they cast a null value then it results is always a null value so why they still need to cast it? This sp is called a million times (&amp;gt;7millions calls).How about a declaring a cursor, what it is overhead cost in terms of performance. They use it in one of their scalar function and being called also several times. this function compares the value of previous record to the current one. the loop will only pass on less than 10 records, and reading to a table containing a millions of records.any comments, suggestion.thanks.[/quote]The CAST function is pretty negligible, I wouldnt focus on it.  The VARBINARY(MAX) has bigger potential issues than the CAST itself.  Unless your using FILESTREAM, the page splits can be a bigger cost.As for CURSORs, they are not evil.  Contrary to what the lemmings say.  It is what you do IN the cursor loop that is the danger.  There are times and places for them, and most of the time they can be replaced with TSQL alternatives.  Knowing when they are the correct tool, and being cognizant of what your doing in the loop can make them reasonably efficient.</description><pubDate>Thu, 21 Feb 2013 06:49:15 GMT</pubDate><dc:creator>DiverKas</dc:creator></item><item><title>RE: TSQL Optimization - CASTing and CURSOR</title><link>http://www.sqlservercentral.com/Forums/Topic1422379-391-1.aspx</link><description>Using a CAST(NULL as &amp;lt;DataType&amp;gt;) is usually done to ensure that a place holder column has the right datatype for a later process especially when doing an SELECT INTO or when doing a Union across 2 tables with different columns, e.g.[code="sql"]Select      Column1     ,column2     ,CAST(NULL as Varchar(50)) as Column3From      Table1UNION ALLSelect      Column1     ,column2     ,Column3Into #tmpFrom      Table2[/code]The other common use is when you are working with RS and SSIS calling SP's that use Temp tables, as these applications have problems 'guessing' the data set so you can fool it by doing this at the top of the proc.[code="sql"]IF 1=0BeginSelect    Cast(NULL as Int) as Column1   Cast(NULL as Decimal(18,2)) as Column2   Cast(NULL as DateTime) as Column3    --- More columnsEnd[/code]</description><pubDate>Thu, 21 Feb 2013 00:30:17 GMT</pubDate><dc:creator>Jason-299789</dc:creator></item><item><title>RE: TSQL Optimization - CASTing and CURSOR</title><link>http://www.sqlservercentral.com/Forums/Topic1422379-391-1.aspx</link><description>definitely conversion has some cost whether it is explicit or implicit. and if the cast/convert  have been used in left part of filter (like cast(last_name as nvarchar(30)) = 'bhuv') then it will also hamper the index usagel.second , why you want to replace the code with cursor ? it wil definitely has some bad impact. Post your code , for better opinion.</description><pubDate>Wed, 20 Feb 2013 23:05:59 GMT</pubDate><dc:creator>Bhuvnesh</dc:creator></item><item><title>TSQL Optimization - CASTing and CURSOR</title><link>http://www.sqlservercentral.com/Forums/Topic1422379-391-1.aspx</link><description>hi,Is CASTing cost an additional overhead to the query? I need to optimize our database initialization and i reviewing some of the sql scripts and found out that there are several sp using this  CAST( NULL AS VARBINARY(MAX)) in defining their column. If they cast a null value then it results is always a null value so why they still need to cast it? This sp is called a million times (&amp;gt;7millions calls).How about a declaring a cursor, what it is overhead cost in terms of performance. They use it in one of their scalar function and being called also several times. this function compares the value of previous record to the current one. the loop will only pass on less than 10 records, and reading to a table containing a millions of records.any comments, suggestion.thanks.</description><pubDate>Wed, 20 Feb 2013 19:59:55 GMT</pubDate><dc:creator>Jay-246689</dc:creator></item></channel></rss>