Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««23456»»

Is It Time To Clear Out Those Quirky Functions? Expand / Collapse
Author
Message
Posted Monday, July 14, 2014 3:48 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:18 PM
Points: 35,267, Visits: 31,759
Andy Warren (7/11/2014)
Jeff, did I just see you plug for Access?


Heh... not quite. I figure if even lowly Access can have an incredible and easy to use PIVOT function, why not SQL Server?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1592367
Posted Monday, July 14, 2014 3:53 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:18 PM
Points: 35,267, Visits: 31,759
Alan.B (7/14/2014)
Jeff Moden (7/10/2014)
Alan.B (7/10/2014)
Phil, you can make this more than twice as fast by turning it into an inline table value function like this:


To that, I say... are you sure? Please read the following article before you answer that question.
http://www.sqlservercentral.com/articles/T-SQL/91724/


I should have posted a different test harness but I was basing that statement on the fact that - on my system the original function runs for 10-11 seconds (11 with results on, 10 with results off). The itvf version runs for 3-5 seconds (3 with results off). Those were the times I got just by just highlighting the code and hitting F5.

If I measure the results like this:

DECLARE @Result char(4);
--===== Begin measuring duration using GETDATE
DECLARE @StartTime DATETIME;
SELECT @StartTime = GETDATE();
--===== Test the code using the "throw-away" variable

PRINT char(13)+'lefttrim:'
SELECT @result = dbo.lefttrim(string)
FROM #test;

--===== "Stop the timer" and report the duration
PRINT DATEDIFF(ms,@StartTime,GETDATE());
GO

DECLARE @Result char(4);
--===== Begin measuring duration using GETDATE
DECLARE @StartTime DATETIME;
SELECT @StartTime = GETDATE();
--===== Test the code using the "throw-away" variable

PRINT char(13)+'itvf_lefttrim:'
SELECT @result = x.string
FROM #test
CROSS APPLY dbo.itvf_LeftTrim(string) x;

--===== "Stop the timer" and report the duration
PRINT DATEDIFF(ms,@StartTime,GETDATE());
GO

Results:

lefttrim:
10703

itvf_lefttrim:
3466


This is on my laptop (4X) 2.6 ghz, 16gb ram, SQL 2014 Ent.


Now that's what I'm talking about. Something that others can run and something that doesn't use SET STATISTICS on a Scalar Funtion. Thanks, Alan.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1592370
Posted Monday, July 14, 2014 3:59 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:18 PM
Points: 35,267, Visits: 31,759
Phil Factor (7/11/2014)
@Alan.B
After reading your comment, I've nervously put the routine in a test harness. It clocked at 0.054 Ms. per execution. I have to say that when I'm optimizing, I go after bigger fish!


It's just such small-fry that used to choke the server I took over because there were many such small fish that got executed hundreds of millions of times per day. If you consider the overall improvement is 3:1, that's not bad especially considering the number of executions over the course of the day.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1592372
Posted Tuesday, July 15, 2014 7:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Yesterday @ 12:12 PM
Points: 4, Visits: 28
These quirky functions keep us TSQL developers special. The ISNUMERIC tests a field, and CAST converts it if you want.
CASE ISNUMERIC(LEFT(a.[CHBID], 7))
WHEN 1
THEN CAST(LEFT(a.[CHBID], 7) AS INT)
ELSE 0
END AS CHBID
Post #1592548
Posted Tuesday, July 15, 2014 7:52 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 19, 2014 2:15 AM
Points: 213, Visits: 854
Jeff Moden (7/14/2014)


It's just such small-fry that used to choke the server I took over because there were many such small fish that got executed hundreds of millions of times per day.


Most .NET apps I see are spamming the database with thousands of calls a second loading and reloading data; whereby the programmers don't even know why it's happening and can't stop it.

So yeah, amen. Tiny improvements (admittedly on known hot spots) can have big improvements
Post #1592573
Posted Friday, July 18, 2014 10:06 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 7:45 AM
Points: 275, Visits: 614
We got hit by ISNUMERIC early. It will convert text that happens to be valid hexadecimal into a positive hit. So we went to PATINDEX to find values that were numeric. So in the background it does conversion checks.
Post #1594140
Posted Tuesday, July 22, 2014 7:49 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:44 AM
Points: 5,472, Visits: 3,260
Perhaps some kind of deprecation warning (with the ability to locally disable - equivalent of an old C styled # command) would be beneficial for script updates. I completely agree that any kind of deprecation should be 2 stepped just like Java and .NET i.e. first marked as deprecated in a release (with warnings raised) then a standard minimum number of releases occur before removal is even allowed, if at all.

Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
Post #1595087
Posted Tuesday, July 22, 2014 12:05 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:18 PM
Points: 35,267, Visits: 31,759
JustMarie (7/18/2014)
We got hit by ISNUMERIC early. It will convert text that happens to be valid hexadecimal into a positive hit. So we went to PATINDEX to find values that were numeric. So in the background it does conversion checks.


I know I'm being a bit pedantic here but you didn't "get hit" by ISNUMERIC... you used ISNUMERIC incorrectly. I'll admit that the MS documentation isn't in-your-face clear about what it doesn't do (it was never meant to be an IsAllDigits function), but it does state exactly what it does do.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1595220
Posted Monday, August 18, 2014 7:12 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, September 22, 2014 7:38 AM
Points: 28, Visits: 130
I would like to see a (T-)SQL function like normalize-space() from XPath, which I believe is slightly more useful than just TRIM, as it deals with all white space characters including sequences in between other characters. It is particularly useful when combining chunks of text.
Possibly there are other scalar functions from XPath worth considering as well.
Post #1604431
Posted Monday, August 18, 2014 7:37 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:44 AM
Points: 5,472, Visits: 3,260
Tavis Reddick (8/18/2014)
I would like to see a (T-)SQL function like normalize-space() from XPath, which I believe is slightly more useful than just TRIM, as it deals with all white space characters including sequences in between other characters. It is particularly useful when combining chunks of text.
Possibly there are other scalar functions from XPath worth considering as well.


Understanding the danger of showing my naivety, surely that would be better served by cleaner data. N'est pas?


Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
Post #1604449
« Prev Topic | Next Topic »

Add to briefcase «««23456»»

Permissions Expand / Collapse