SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Is It Time To Clear Out Those Quirky Functions?


Is It Time To Clear Out Those Quirky Functions?

Author
Message
GPO
GPO
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1754 Visits: 1938
This may be what GPO noted previously.


Yep. Me and many many others.

:-)

One of the symptoms of an approaching nervous breakdown is the belief that one's work is terribly important.
Bertrand Russell

GPO
GPO
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1754 Visits: 1938
@GoofyGuy
PRETTIFY('phil factor')


Hahahahaha. Maybe there's a reason he only allows us to envisage him through some obscure sepia photo.

:-)

One of the symptoms of an approaching nervous breakdown is the belief that one's work is terribly important.
Bertrand Russell

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223755 Visits: 42006
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223755 Visits: 42006
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223755 Visits: 42006
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
alan.spillert
alan.spillert
SSC-Enthusiastic
SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)

Group: General Forum Members
Points: 129 Visits: 138
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
Cody Konior
Cody Konior
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1099 Visits: 1111
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 :-P
JustMarie
JustMarie
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2223 Visits: 1362
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. Ermm
Gary Varga
Gary Varga
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27779 Visits: 6557
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!!!
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223755 Visits: 42006
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. Ermm


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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search