代码之家  ›  专栏  ›  技术社区  ›  Joshua Hayworth

如何确定存储过程是否每次都在重新编译?

  •  0
  • Joshua Hayworth  · 技术社区  · 16 年前

    我有一个SQL Server存储过程,其中包含以下T-SQL代码:

    insert into #results ([ID], [Action], [Success], [StartTime], [EndTime], [Process])
    select
        'ID' = aa.[ActionID],
        'Action' = cast(aa.[Action] as int),
        'Success' = aa.[Success],
        'StartTime' = aa.[StartTime],
        'EndTime' = aa.[EndTime],
        'Process' = cast(aa.[Process] as int)
    from
        [ApplicationActions] aa with(nolock)
    where
        0 = case
                when (@loggingLevel = 0) then 0
                when (@loggingLevel = 1 and aa.[LoggingLevel] = 1) then 0
            end
        and
        1 = case
                when (@applicationID is null) then 1
                when (@applicationID is not null and aa.[ApplicationID] = @applicationID) then 1
            end
        and
        2 = case
                when (@startDate is null) then 2
                when (@startDate is not null and aa.[StartTime] >= @startDate) then 2
            end
        and
        3 = case
                when (@endDate is null) then 3
                when (@endDate is not null and aa.[StartTime] <= @endDate) then 3
            end
        and
        4 = case
                when (@success is null) then 4
                when (@success is not null and aa.[Success] = @success) then 4
            end
        and
        5 = case
                when (@process is null) then 5
                when (@process is not null and aa.[Process] = @process) then 5
            end
    

    我如何使用SQLServerStudio或探查器来测试此存储过程是否每次都在重新编译?

    3 回复  |  直到 16 年前
        1
  •  2
  •   Cade Roux    16 年前

    简单地说,您可以简化这些:

        2 = case
                        when (@startDate is null) then 2
                        when (@startDate is not null and aa.[StartTime] >= @startDate) then 2
                end
    

    为此:

        (@startDate is null OR aa.[StartTime] >= @startDate)
    

    就重新编译而言,它是声明的吗 WITH RECOMPILE ?

        2
  •  1
  •   Micky McQuade    16 年前

    下面的文章解释了如何确定存储过程是否正在重新编译: http://it.toolbox.com/blogs/programming-life/sql-performance-abnormal-stored-procedure-recompiles-8105

    启动SQL事件探查器并启动新的 跟踪、连接到我们的服务器并给出 “事件”选项卡并删除已存在的事件 “选定事件”上的现有事件 中的“存储过程”节点 添加“可用的事件”和“可用的事件” SPComplete、SPRecompile、SPSSTARTING、, 现在选择“数据列”选项卡并 选择适当数量的 你收集的事件。

    我将按存储过程的名称进行筛选。

        3
  •  1
  •   Rich Andrews    16 年前

    在示例中,插入临时表会导致每次重新编译SP,因为它无法预编译。

    这是使用临时表和表变量之间的区别之一——可以找到一篇关于这些区别的好文章 here

    相关摘录。。。

    第二个主要区别是 有临时表的任何程序 无法预编译,而 表变量可以是静态的 脚本为其 执行速度。这种优势可以