存储过程如下:
CREATE   procedure proc_get_list
@type_code varchar(10)  --类型代码as
set nocount onselect prodcut.name,product.pic from product where prodcut.type_code like @type_code+'%' GO执行 procedure proc_get_list '2001' 需要11秒如果换成下面的
CREATE   procedure proc_get_list2
@type_code varchar(10)  --类型代码as
set nocount ondeclare @type_code2 varchar(10)
set @type_code2=@type_codeselect prodcut.name,product.pic from product where prodcut.type_code like @type_code2+'%' GO
执行procedure proc_get_list2 '2001' 需要0秒把SQL单独复制出来
declare @type_code2 varchar(10)
set @type_code2='2001'select prodcut.name,product.pic from product where prodcut.type_code like @type_code2+'%' 执行需要0秒,请问为什么第一种的速度这么慢.
已经在表prodcut的type_code上建立了非聚焦索引,尝度过去掉索引,换成聚焦索引,速度没有变化.

解决方案 »

  1.   

    执行计划缓存的问题。2种解决方法1). EXEC proc_get_list '2001' WITH RECOMPILE2). procedure中使用动态SQL.
      

  2.   

    运行完第一次后 存在 执行计划缓存再次运行存储过程 带上RECOMPILE选项
    RECOMPILE
    指示数据库引擎不缓存该过程的计划,该过程在运行时编译。如果指定了 FOR REPLICATION,则不能使用此选项。对于 CLR 存储过程,不能指定 RECOMPILE。若要指示数据库引擎放弃存储过程内单个查询的计划,请使用 RECOMPILE 查询提示。有关详细信息,请参阅查询提示 (Transact-SQL)。如果非典型值或临时值仅用于属于存储过程的查询子集,则使用 RECOMPILE 查询提示。
      

  3.   

    不是执行第几次的问题,不论执行多少次,两个存储过程的速度都有明显的差异,另外重启MSSQL也一样.楼上几位说的缓存,我再看看
      

  4.   

    有没有一张产品类型prodcut.type_code 表,如果有的话这句还是改改的好!不然在主表使用like的话效率太低了。