• scziege (7/2/2009)


    The stored procedure calls a user-defined function which generates the code. But this is not

    the reason why the performance is so bad.

    I would encourage you to test that. The script below will allow you to see how expensive the function call is. It is set up to call a test function that doubles a number. The function is executed one million times per run, and there are five runs. You get summary performance statistics at the end. There are comments to explain what it is doing, and where to plug your private function in.

    Paul

    use tempdb;

    go

    -- Holds numbers 1 to 1 million

    createtable [#418BC2CC-48A9-4BCF-9E18-B8FB7BE9D446]

    (

    NBIGINT PRIMARY KEY CLUSTERED,

    );

    -- Generate numbers

    withNumbers (N)

    as(

    selecttop (1000000)

    ROW_NUMBER() over (order by (select null))

    frommaster.sys.columns C1, master.sys.columns C2, master.sys.columns C3

    )

    insert[#418BC2CC-48A9-4BCF-9E18-B8FB7BE9D446] with (tablockx) (N)

    selectN

    fromNumbers;

    go

    -- Test function - just doubles the input

    create function dbo.[sfn_7125CD1E-CF5A-4386-B82C-CF52AC756A71] (@number bigint) returns bigint with schemabinding as begin return @number * 2; end;

    go

    -- Clear ad-hoc sql plans (warning: server-wide, run on test only!)

    dbcc freesystemcache(N'SQL Plans');

    go

    -- Run function one million times - substitute your function here!

    declare@Bitbucket BIGINT;

    select@Bitbucket = dbo.[sfn_7125CD1E-CF5A-4386-B82C-CF52AC756A71] (N)

    from[#418BC2CC-48A9-4BCF-9E18-B8FB7BE9D446];

    go 5

    -- Show test results

    selectstatement_text = SUBSTRING([text], statement_start_offset / 2, (case statement_end_offset when -1 then DATALENGTH(text) else statement_end_offset end) / 2),

    sample_size = execution_count,

    [avg elapsed time µs] = total_elapsed_time / execution_count,

    [avg elapsed time sec] = CONVERT(DEC(9, 3), total_elapsed_time / execution_count / 1000000.0),

    [avg cpu time µs] = total_worker_time / execution_count,

    [avg cpu time sec] = CONVERT(DEC(9, 3), total_worker_time / execution_count / 1000000.0),

    [avg logical reads] = total_logical_reads / execution_count

    fromsys.dm_exec_query_stats as qs

    cross

    applysys.dm_exec_sql_text (qs.[sql_handle]) as st

    wherest.[text] like '%@BitBucket%[#418BC2CC-48A9-4BCF-9E18-B8FB7BE9D446]%'

    andst.[text] not like '%dm_exec_query_stats%';

    go

    -- Tidy up

    drop table [#418BC2CC-48A9-4BCF-9E18-B8FB7BE9D446];

    drop function dbo.[sfn_7125CD1E-CF5A-4386-B82C-CF52AC756A71];

    go