|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Monday, January 14, 2013 4:54 AM
Points: 835,
Visits: 72
|
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Tuesday, June 11, 2013 5:01 AM
Points: 4,816,
Visits: 1,343
|
|
The article was nice. I hopoe the code is right. But when I tried to run the code I am getting error in the Insert statement.
BEGIN
INSERT INTO CaseDetail VALUES(200,300,1)
SET @count = @count -1
END
I am not having time to check the error but do we require to write the column names in the syntax. I may done some silly mistakes also.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, March 27, 2009 4:01 AM
Points: 14,
Visits: 40
|
|
The final select statement where you use a local variable is different to the pure select statement, it only works because you've got all the values exactly the same. So the examples you give are broken because you've been lazy and used exactly the same values.
If your data really is like that something's wrong with the data structure.
All this article shows is how you should never use functions unless you know exactly what they do, precisely the opposite of what you say at the beginning. In this case the function is so useless you might as well throw it away.
My attitude with functions is avoid like the plague, as they break the rule of thinking in sets while programming SQL.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Monday, January 14, 2013 4:54 AM
Points: 835,
Visits: 72
|
|
Hi Paul,
The following chunk of code worked fine for me.
CREATE TABLE CaseDetail (CaseId INT IDENTITY(1,1), QTY NUMERIC(14,4), weightedQty NUMERIC(14,4), ShipmentId INT)
--We assume that these cases belongs to Shipment with id 1
DECLARE @count INT SET @count =15000
WHILE (@count >0) BEGIN INSERT INTO CaseDetail VALUES(200,300,1) SET @count = @count -1 END
select count(caseid) from CaseDetail
Thank you and have a nice day, Sreeju
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, January 02, 2013 12:15 PM
Points: 1,443,
Visits: 711
|
|
There may be a few instances where functions can speed things along, I've found most of the time they hinder performance instead of helping it.
The times they've been useful to me is when I inherit something that's been designed poorly, and theres no $$$ to redesign it, but I have to make something work.
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Tuesday, June 11, 2013 5:01 AM
Points: 4,816,
Visits: 1,343
|
|
Hi Sreeju
I may be wrong. May typed wrongly. I will check it again. Thnks for the info ......
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 1:06 AM
Points: 13,437,
Visits: 25,282
|
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Yesterday @ 2:35 PM
Points: 3,840,
Visits: 5,641
|
|
My own rule of thumb is to avoid functions that contain queries. Yes, they make for easy reading, but it's far too likely that they will get and reused in SELECT queries. RBAR bad.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller Stop, children, what's that sound? -- Stephen Stills
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 1:11 PM
Points: 45,
Visits: 133
|
|
What about matrix type calculations? How can they be accelerated? What I mean is you have a bunch (about a dozen) of tables with charts of tax rates,freight rates, etc. Each row will have different values from each table and then they get computed for the final result. We have functions for various lookups and calculations, some will be repeated, some won't How can you improve this type of process? I can see your technique for the 1 function, but I'm not sure how it will scale in a large stored proc with lots of function calls and computations.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Thursday, May 02, 2013 5:38 AM
Points: 870,
Visits: 858
|
|
I don't know.... I've run both your inline and variable procedures and on my machine they both take about 470ms, give or take a few ms. In fact, if anything I've found the variable method slightly slower by up to 50ms.
Did you dropcleanbuffers before running each procedure? If not then you might find that the cached results are interfering with your numbers.
Also, if you check out the io statistics, more logical reads are performed with the variable method.
I'm not sure I see the benefit in running the exact same query twice, as you're doing in the variable method. Unless I'm missing something, which is quite possible ;)
Karl source control for SQL Server
|
|
|
|