存储过程运行超时,无法执行下去的问题

catliang 2006-01-04 09:49:32
程序调用的某sp,sp中有用到临时表,和相关很多表,临时表数据量在上万条,有临时表和临时表关联,当执行到某句insert语句时,用跟踪发现,就停在那边,无法执行下去,但是单独把那句话单独拿出来,只要几秒就insert了。
发现此问题一个重要的前提是,一张table,如果不为空,则sp执行的很顺利,速度挺快,当这张table为空,第一次执行,就会停住了(当然不是不计算,只是时间的长度实在太长)

请高手帮忙,有没有遇到此类情况??十分感谢,此问题困扰多时,不解决的话。。。。
...全文
150 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kevin521 2006-01-04
  • 打赏
  • 举报
回复
估计sp有问题,如果执行sp还超时,准备优化吧
OracleRoob 2006-01-04
  • 打赏
  • 举报
回复
此表有关键字吗
lw1a2 2006-01-04
  • 打赏
  • 举报
回复
不贴出代码,是不是很长呀?
catliang 2006-01-04
  • 打赏
  • 举报
回复
问题发现了。是因为sp中用了事务!
去掉事务,就可以很快执行完了。当然为什么加了事务有这么个情况,暂时还是不知道
特别是,有事务的情况下,第二次执行,还是可以很快。但第一次就十分慢,在当数据量很大的时候(参数范围选的大)
catliang 2006-01-04
  • 打赏
  • 举报
回复
代码贴的比较乱,估计对理解没什么用,碰到过的,知道原因的就谢谢了。
不然,的确很难回答我的问题
catliang 2006-01-04
  • 打赏
  • 举报
回复
那就举个例子,当ashiftstatist为空是,在sp中就是插入不了,看到的跟踪是指执行到这个位置,单独拿出来执行30000多条记录,4s搞定了。
Insert Into aShiftStatist(Term,Badge,DepID,GroupID,Shift
,AttendID,aLongTime,sLongTime,FLongTime,xUnit)
Select a.Term,a.Badge,a.DepID,a.GroupID,a.Shift
,N'WorL',Isnull(c.xHours,0) - Isnull(b.aLongTime,0),
Isnull(c.xHours,0) - Isnull(b.sLongTime,0),
Isnull(c.xHours,0) - Isnull(b.FLongTime,0),
N'H'
From aShiftAttendance a Left Join
(Select a.Badge,a.Term,
Sum(Case a.xUnit When 'D' Then Isnull(a.sLongTime,0)*Isnull(b.xHours,0)
When 'H' then Isnull(a.sLongTime,0)
When 'M' Then Isnull(a.sLongTime,0)/60.0
Else 0
End) sLongTime,
Sum(Case a.xUnit When 'D' Then Isnull(a.aLongTime,0)*Isnull(b.xHours,0)
When 'H' then Isnull(a.aLongTime,0)
When 'M' Then Isnull(a.aLongTime,0)/60.0
Else 0
End) aLongTime,
Sum(Case a.xUnit When 'D' Then Isnull(a.FLongTime,0)*Isnull(b.xHours,0)
When 'H' then Isnull(a.FLongTime,0)
When 'M' Then Isnull(a.FLongTime,0)/60.0
Else 0
End) FLongTime
From aShiftStatist a,aShift_Type b
Where a.Shift = b.Shift
And a.AttendID <> 'RevS'
And a.AttendID <> 'ShWL'
And a.AttendID Not In (Select OTID From aOT_Type)
And a.Shift <> 'R'
Group by a.Badge,a.Term) b On a.Badge = b.Badge And Datediff(Day,a.Term,b.Term) = 0
Inner Join aShift_Type c ON a.Shift = c.Shift
Where a.Shift <> 'R'

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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