Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Scalar UDF different performance on different server


Scalar UDF different performance on different server

Author
Message
mister.magoo
mister.magoo
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2266 Visits: 7820
shahgols (11/8/2013)
This is a shot in the dark, but I am putting it out there just in case.

I compared the actual execution plans and the XML of the staging execution plan contains these weird characters after the WHERE clause that don't exist in my query (or the query would obviously fail). Anyone know what these are?

StatusId not in ('S','O','L') "


Just CR/LF pairs ...

MM


select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);




  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • shahgols
    shahgols
    SSChasing Mays
    SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)

    Group: General Forum Members
    Points: 647 Visits: 5727
    You are right, that's what they were.... Sad

    I was hoping they were something more important, and they would be the reason why my query runs faster in staging. :-P



    mister.magoo
    mister.magoo
    SSCrazy
    SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

    Group: General Forum Members
    Points: 2266 Visits: 7820
    What does the UDF in question do? It may be pertinent.

    MM


    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);




  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Bhuvnesh
    Bhuvnesh
    SSCrazy
    SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

    Group: General Forum Members
    Points: 2924 Visits: 4076
    TheSQLGuru (11/8/2013)
    the engine LIES to SSMS when UDFs are in play.
    please elaborate. Or ANy article reference.

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
    TheSQLGuru
    TheSQLGuru
    SSCertifiable
    SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

    Group: General Forum Members
    Points: 5935 Visits: 8297
    shahgols (11/8/2013)
    I really appreciate your response and patience!

    If I run Profiler, the sql statement will never finish. haha

    I am using sp_whoisacitve. the CPU usage is much, muuuuch higher on production than it is on staging. And the "reads" column is somewhat higher. On staging, where the script runs in 25 seconds, the CPU shows 19K cycles, and reads shows 45K reads. On production, after 3 hours of running the same script, the CPU is at 9M cycles, and reads is at 64K reads.

    I hope that answers your question, otherwise let me know what to do (that doesn't involve Profiler) please.


    My guess is that you are running Profiler with STATEMENT completed as opposed to BATCH completed events. NEVER run profiler with STATEMENT completed if you have cursors, UDFs, etc in your production system. BATCH completed won't cause the query to run slowly by trying to capture every execution of the guts of the UDF like STATEMENT completed will.

    Best,

    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru at GMail
    Jeff Moden
    Jeff Moden
    SSC-Forever
    SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

    Group: General Forum Members
    Points: 44871 Visits: 39850
    Bhuvnesh (11/8/2013)
    TheSQLGuru (11/8/2013)
    the engine LIES to SSMS when UDFs are in play.
    please elaborate. Or ANy article reference.


    Here's the proverbial tip of the iceberg on that subject...
    http://www.sqlservercentral.com/articles/T-SQL/91724/

    --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.
    Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
    Although change is inevitable, change for the better is usually not.
    Just because you can do something in PowerShell, doesnt mean you should. Wink

    Helpful Links:
    How to post code problems
    How to post performance problems
    Forum FAQs
    tomartbd
    tomartbd
    Forum Newbie
    Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

    Group: General Forum Members
    Points: 3 Visits: 4
    Thanks to knowing about it
    shahgols
    shahgols
    SSChasing Mays
    SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)

    Group: General Forum Members
    Points: 647 Visits: 5727
    Bhuvnesh (11/8/2013)
    TheSQLGuru (11/8/2013)
    the engine LIES to SSMS when UDFs are in play.
    please elaborate. Or ANy article reference.


    Below is the UDF:

    CREATE FUNCTION [dbo].[CheckDigit](@vol varchar(50))
    returns int
    as
    begin


       declare @trdx bigint,
          @i int
          
       select @i = 0,
          @trdx = 0,
          @vol = replace(@vol,'.','')
       
       if isNumeric(@vol) = 1
       begin
       
          while len(@vol) > 0
          begin
             
             if @i in (0,1)
             begin
                select @i = 3
             end
             else if @i = 3
             begin
                select @i = 7
             end
             else if @i = 7
             begin
                select @i = 1
             end
             
             
             select @trdx = @trdx + left(@vol,1) * @i
       
             if LEN(@vol) = 1
             begin
                select @vol = ''
             end
             else
             begin
                select @vol = substring(@vol,2,len(@vol))
             end
             
          end
       end
       
       
       return case when @trdx % 11 = 0 then 1
          when @trdx % 11 = 1 then 0
          else 11 - @trdx % 11
          end
       
    end




    TheSQLGuru
    TheSQLGuru
    SSCertifiable
    SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

    Group: General Forum Members
    Points: 5935 Visits: 8297
    1) Yep - that's a scalar UDF alright :-D

    2) At least it doesn't have ancillary data access!

    3) Looks like some kind of funky iterative logic that might be able to take advantage of a Numbers table

    4) How is it used in the query? If in the WHERE clause you are pretty much guaranteed to be hosed.

    5) I wonder if a persisted calculated column would be workable here...

    Best,

    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru at GMail
    shahgols
    shahgols
    SSChasing Mays
    SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)SSChasing Mays (647 reputation)

    Group: General Forum Members
    Points: 647 Visits: 5727
    Those are good workarounds, and it might be what we will end up doing. The SQL statement is something like this:

    select someID, right ('0000000000' + convert(varchar(10), a.someID, 10)) +
    convert (varchar, dbo.CheckDigit(right('0000000000' _ convert(varchar, someID),10)))
    from my_table as a inner join some_other_table b on blah = blah
    where some_column not in ('a', 'b', 'c');


    EDIT: Both "blah" columns are bigint, and have a clustered index on them.



    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