重新编译存储过程
在用诸如添加索引或更改索引列中的数据等操作更改数据库时,应通过重新编译访问数据库表的原始查询计划使其得到重新优化。在重新启动 Microsoft® SQL Server™ 2000 后第一次运行存储过程时自动发生该优化。当存储过程使用的基础表更改时也会发生优化。但是如果添加了存储过程可能从中受益的新索引,将不发生自动优化,直到下一次 SQL Server 重新启动后再运行该存储过程时为止。SQL Server 提供三种重新编译存储过程的方法: sp_recompile 系统存储过程强制在下次运行存储过程时进行重新编译。
创建存储过程时在其定义中指定 WITH RECOMPILE 选项,表明 SQL Server 将不对该存储过程计划进行高速缓存;该存储过程将在每次执行时都重新编译。当存储过程的参数值在各次执行间都有较大差异,导致每次均需创建不同的执行计划时,可使用 WITH RECOMPILE 选项。此选项并不常用,因为每次执行存储过程时都必须对其进行重新编译,这样会使存储过程的执行变慢。
在执行存储过程时指定 WITH RECOMPILE 选项,可强制对存储过程进行重新编译。仅当所提供的参数不典型,或者自创建该存储过程后数据发生显著更改时才应使用此选项。 
说明  如果删除或重命名了存储过程所引用的对象,那么在执行该存储过程时会返回错误。但如果用同名对象替换了存储过程中引用的对象,则可以不进行重新编译而继续执行该存储过程。

解决方案 »

  1.   

    比如说有一个存储过程sp_date(日期参数),使用的时候,参数经常是在变的,那么它建立的时候是否就应recompile,我这样理解对不对啊?
      

  2.   

    减少执行过程中的重新编译对有的查询而言,由编译过程产生查询计划所付出的代价占是执行整个查询所付出代价的一部分,所以使用事先编译好的计划可以节省时间,避免重新编译的情况存储过程recompile的原因referenced objects, running the sp_recompile system stored procedure against
    a table referenced by the stored procedure, 
    restoring the database containing the stored procedure or any object referenced by the stored procedure, or the stored 
    procedures plan dropping from the cache. 
    删除或者重建过程
    在过程里使用with recomplie语句,或者在执行时使用
    使用sp_recomlile 使存储过程在下次运行时重新编译
    恢复数据库时
    或者存储过程计划从高速缓存中移出
    如果过程引用表的有足够的数据发生变化
    如果用户在DDL语句中插入DML语句
    SET CONCAT_NULL_YIELDS_NULL While these recompilations are normal and cannot be helped, DBAs and developers 
    should not assume that all stored procedure recompiles are for normal reasons and should take a proactive approach to determine if they have a recompile problem. 
    可以使用profile跟踪过程的重新编译
    新建一跟踪
    事件删除全部,选择存储过程下的 SP:Recompile, SP:Starting, and SP:Completed under Stored  Procedure events
    sP:StmtStarting and SP:StmtCompleted 可以查看哪些语句引起recompileMicrosoft关于最小化应用程序的重新编译问题
    Troubleshoting Stored Procedure Recompilaion
    http://support.microsoft.com/support/kb/articies/q243/5/86.asp