|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: 2 days ago @ 10:24 PM
Points: 6,
Visits: 31
|
|
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 (>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.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, March 26, 2013 8:41 AM
Points: 2,562,
Visits: 3,451
|
|
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.
-------Bhuvnesh---------- While 1 = 1 (Learning SQL....) Click to get fast response of your post
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: 2 days ago @ 1:30 AM
Points: 803,
Visits: 2,124
|
|
Using a CAST(NULL as <DataType>) 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.
Select Column1 ,column2 ,CAST(NULL as Varchar(50)) as Column3 From Table1 UNION ALL Select Column1 ,column2 ,Column3 Into #tmp From Table2
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.
IF 1=0 Begin Select Cast(NULL as Int) as Column1 Cast(NULL as Decimal(18,2)) as Column2 Cast(NULL as DateTime) as Column3 --- More columns End
_________________________________________________________________________ SSC Guide to Posting and Best Practices
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 10:24 AM
Points: 237,
Visits: 413
|
|
Jay-246689 (2/20/2013) 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 (>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.
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.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 3:56 PM
Points: 1,324,
Visits: 1,778
|
|
Is CASTing cost an additional overhead to the query?
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.
SQL DBA,SQL Server MVP('07, '08, '09) One man with courage makes a majority. Andrew Jackson
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: 2 days ago @ 10:24 PM
Points: 6,
Visits: 31
|
|
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, >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.
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Yesterday @ 11:07 PM
Points: 21,625,
Visits: 27,468
|
|
Jay-246689 (3/20/2013) 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, >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.
A little hard to offer advice on how to rewrite a cursor when you don't even provide the entire code.
 Lynn Pettis
For better assistance in answering your questions, click here For tips to get better help with Performance Problems, click here For Running Totals and its variations, click here or when working with partitioned tables For more about Tally Tables, click here For more about Cross Tabs and Pivots, click here and here Managing Transaction Logs
SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
|
|
|
|