Is It Time To Clear Out Those Quirky Functions?

  • aochss (7/10/2014)


    Please, please make the error/warning messages better and more precise...Nothing worse than something like this:

    Msg 8152, Level 16, State 14, Line 1

    String or binary data would be truncated.

    The statement has been terminated.

    Come on SQL Server, you know where the error is, why not share?

    Anton

    Amen to that!

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • The one that I ran into today building a sproc. I have data that is imported into staging table. But depending on the customer they can give us data with an arbitrary staff_id_number or the identifier_NPI. The import routine determines which it is getting and creates ne column r the ther but not both in the same table.

    So I build the sproc with an if statement that checks which column is there and use one query or the other. It parses fine, but then when trying to execute it chokes on the missing column name. I had to go to dynamic SQL to get it to run. AAARRRRGGGHHHH!!!! :crying:



    ----------------
    Jim P.

    A little bit of this and a little byte of that can cause bloatware.

  • Nevermind.

  • aochss (7/10/2014)


    Please, please make the error/warning messages better and more precise...Nothing worse than something like this:

    Msg 8152, Level 16, State 14, Line 1

    String or binary data would be truncated.

    The statement has been terminated.

    Come on SQL Server, you know where the error is, why not share?

    Anton

    There's actually been an MS CONNECT item open on that very subject. It almost has 800 vote-ups, has been open for years, and yet MS has done nothing with it.

    --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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (7/10/2014)


    There's actually been an MS CONNECT item open on that very subject. It almost has 800 vote-ups, has been open for years, and yet MS has done nothing with it.

    Wouldn't you love to be a fly on the wall and find out exactly how this kind of thing goes on? I can just imagine some terrible internal architecture that makes passing that object name along almost impossible... or an obstinate manager saying, "It's always been like this!"... or a revolving door of staff entering and leaving the project leaving nobody skilled enough to get the job done... or just such an influx of stupid time-draining requests combined with a management demand to see an ROI case on every change that nothing important can be done.

    Which nightmare scenario is it Microsoft? One? Two? All? ๐Ÿ˜€

  • Jeff, did I just see you plug for Access?:-)

    Nice post Phil. I wish we saw more public discussion on this.

  • @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!

    @Andy Warren

    It was when I saw MSDN actually warn people that IsNumeric() 'returns 1 for some characters that are not numbersโ€™ that I blinked in astonishment and thought it was time they did some house-keeping.

    Best wishes,
    Phil Factor

  • 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 APPLYdbo.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.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • 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 ones work is terribly important.... Bertrand Russell

  • @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 ones work is terribly important.... Bertrand Russell

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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 APPLYdbo.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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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

  • 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 ๐Ÿ˜›

Viewing 15 posts - 31 through 45 (of 54 total)

You must be logged in to reply to this topic. Login to reply