奇怪的存储过程

wyj13309 2008-07-17 08:34:18
存储过程:

create procedure da_xm
@ipdwname varchar(50)
as
declare @t_ipdwcode varchar(50);
begin
select top 1 @t_ipdwcode=aaaa from test1 where abcd=@ipdwname;

select sum(abcd) from test
where aa=@t_ipdwcode
and gltr_project<>''
and year(gltr_eff_dt)=year(getdate());
end

说明:test中有1000万以上条数据,
奇怪的问题,如果将值@t_ipdwcode以手工方式填入过程,执行速度为1秒,但如果通过传入变量的方式传入,要等待2分钟以上.
不知何原因?
...全文
109 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
llj0209013 2008-07-17
  • 打赏
  • 举报
回复



是在应用程序中通过传入变量的方式传入,要等待2分钟以上?还是在SQL查询分析器中?

bwu851 2008-07-17
  • 打赏
  • 举报
回复
索引? 可能吧.

但是, LZ手工输入运行时间很短, 说明索引是没什么问题的.
中国风 2008-07-17
  • 打赏
  • 举报
回复
aa,gltr_project,gltr_eff_dt--建一個組合索引

create index IX_test on test(aa,gltr_project,gltr_eff_dt)
bwu851 2008-07-17
  • 打赏
  • 举报
回复
你这个不是连接的问题. 是著名的"parameter sniffing"问题.

解决办法有:
1. 用WITH Recompile, 如

EXEC da_xm @ipdwname = 'abced' WITH RECOMPILE;


在2005里有: 2. Query hint RECOMPILE, 如:

select sum(abcd) from test
where aa=@t_ipdwcode
and gltr_project <>''
and year(gltr_eff_dt)=year(getdate())
OPTION (RECOMPILE);


3. 用OPTIMIZE FOR, 如:

select sum(abcd) from test
where aa=@t_ipdwcode
and gltr_project <>''
and year(gltr_eff_dt)=year(getdate())
OPTION (OPTIMIZE FOR (@t_ipdwcode = 'abcde'));

linsyuan 2008-07-17
  • 打赏
  • 举报
回复
可能是没建索引
Novelty 2008-07-17
  • 打赏
  • 举报
回复
在test1表使用abcd字段建立索引.
qqhmitzk 2008-07-17
  • 打赏
  • 举报
回复
应该是程序处理变量,连接的问题
wgzaaa 2008-07-17
  • 打赏
  • 举报
回复
--看看执行这句要花多长时间
select top 1 @t_ipdwcode=aaaa from test1 where abcd=@ipdwname
hyde100 2008-07-17
  • 打赏
  • 举报
回复
是程序连接传变量慢吧?正常,连接问题
wyj13309 2008-07-17
  • 打赏
  • 举报
回复
up.

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧