跪求高手帮忙解决,优化存储过程!
现在出现了如下问题:
1、存储过程执行时间长达5-15分钟,占用很大的CPU,使其它程序无法继续;
2、经常报错或执行不了。比如退货时没有修改成负数,还是正数。
请各位大虾帮忙解决!看如何优化此存储过程,谢谢!
/*---------------------
入库出库单审核时
更新商品目录表
更新库存表
*/
CREATE PROCEDURE pcargosh(@orderno varchar(50),@orderlb varchar(50),@shrr varchar(50))AS
set nocount on
if (@orderlb='TH')or(@orderlb='SH')
begin
update pbscitems set quantity=-quantity,amount=-amount where orderno=@orderno
update pcargo set quantity=-quantity,subtotal=-subtotal, mysubtal=-mysubtal where orderno=@orderno
end
declare @storename varchar(50)
declare @storeno1 varchar(50)
declare @storemax int
select @storename=storename,@storeno1=storeno1 from pcargo where orderno=@orderno
if @orderlb='PD'
begin
update pbscitems set storename=@storename where orderno=@orderno
select bookid,sum(isnull(quantity,0)) quantity,storename into #tmp1 from pbscitems where orderno=@orderno and lb=@orderlb group by bookid,storename
declare @maxpcb int
select @maxpcb=isnull(max(id),0) from pcb
select identity(int,1,1) id,bookid,quantity,storename into #tmp2 from #tmp1 where bookid not in(select bookid from pcb where storename=@storename)
insert into pcb(id,bookid,storename)select @maxpcb+id,bookid,storename from #tmp2
update pcb set quantity1=quantity1+#tmp1.quantity from #tmp1 where #tmp1.bookid=pcb.bookid and #tmp1.storename=pcb.storename
return 0
end
declare @shr varchar(50)
select @shr=shr from pcargo where orderno=@orderno and lb=@orderlb
if @shr is not null
return 0
update pbscitems set storename=@storename where orderno=@orderno and (storename is null or storename='')
BEGIN TRAN T1
create table #tmptb(bookid int null,sl float null,zje float null,storename varchar(50) null)
insert into #tmptb(bookid,sl,zje,storename)select pbscitems.bookid,sum(isnull(pbscitems.quantity,0)),sum(isnull(pbscitems.quantity*cargo.price,0)),pbscitems.storename from pbscitems,cargo where pbscitems.bookid=cargo.cargo_id and pbscitems.orderno=@orderno group by pbscitems.bookid,pbscitems.storename
/*insert into #tmptb(bookid,sl,zje,storename)select bookid,sum(isnull(quantity,0)),sum(isnull(amount,0)),storename from pbscitems where orderno=@orderno group by bookid,storename*/
select @storemax=isnull(max(id),0) from currentstore
select identity(int,1,1) id,bookid,storename into #tmptb1 from #tmptb where bookid not in(select bookid from currentstore where storename<>@storeno1)
select identity(int,1,1) id,bookid into #tmptb2 from #tmptb where bookid not in(select bookid from currentstore where storename=@storeno1)
update pcargo set hkye=subtotal,fpjf='否' where orderno=@orderno
/*
更新在单数
*/
if (@orderlb='S')or(@orderlb='B')
update orderit set onsave=isnull(orderit.onsave,0)+pbscitems.quantity from pbscitems where pbscitems.orderbh=orderit.bh and orderit.id=pbscitems.orderid and pbscitems.orderno=@orderno
if(@orderlb='B')or(@orderlb='PY')or(@orderlb='TH')or(@orderlb='QC')
begin
/* 更新目录表信息*/
update cargo set quantity=quantity+#tmptb.sl,amount=amount+#tmptb.zje from #tmptb where cargo.cargo_id=#tmptb.bookid
end
if @orderlb='B'
begin
update cargo set onorder=onorder-tmpsl.sl from (select bookid,sum(quantity) sl from pbscitems where orderno=@orderno and orderbh is not null group by bookid) tmpsl where tmpsl.bookid=cargo.cargo_id
update cargo set avgprice=amount/quantity where cargo_id in(select bookid from #tmptb) and quantity<>0
update cargo set avgprice=0 where quantity=0
/*更新库存分布表*/
select identity(int,1,1) id,bookid,storename into #tmptb3 from #tmptb where bookid not in(select bookid from currentstore where storename in(select storename from pbscitems where orderno=@orderno group by storename))
insert into currentstore(id,bookid,storename)select @storemax+id,bookid,storename from #tmptb3
update currentstore set quantity=quantity+#tmptb.sl,amount=amount+#tmptb.zje from #tmptb where currentstore.bookid=#tmptb.bookid and currentstore.storename=#tmptb.storename
update cust set ys=isnull(cust.ys,0)+isnull(pcargo.subtotal,0) from cust,pcargo where orderno=@orderno and cust.custno=pcargo.custno
end
if(@orderlb='S')or(@orderlb='PK')or(@orderlb='SH')
begin
update cargo set quantity=quantity-#tmptb.sl from #tmptb where cargo.cargo_id=#tmptb.bookid
end
if @orderlb='S'
begin
update cargo set onorder1=onorder1-tmpsl.sl from (select bookid,sum(quantity) sl from pbscitems where orderno=@orderno and orderbh is not null group by bookid) tmpsl where tmpsl.bookid=cargo.cargo_id
update cargo set amount=quantity*price where cargo_id in(select bookid from #tmptb)
/*更新库存分布表*/
select identity(int,1,1) id,bookid,storename into #tmptb4 from #tmptb where bookid not in(select bookid from currentstore where storename in(select storename from pbscitems where orderno=@orderno group by storename))
insert into currentstore(id,bookid,storename)select @storemax+id,bookid,storename from #tmptb4
update currentstore set quantity=quantity-#tmptb.sl from #tmptb where currentstore.bookid=#tmptb.bookid and currentstore.storename=#tmptb.storename
update currentstore set currentstore.amount=currentstore.quantity*cargo.price from cargo where cargo.cargo_id=currentstore.bookid and currentstore.bookid in(select bookid from #tmptb)
update cust set ys=isnull(cust.ys,0)+isnull(pcargo.subtotal,0) from cust,pcargo where orderno=@orderno and cust.custno=pcargo.custno
end
if(@orderlb='TB')
begin
/*更新出库数量*/
update currentstore set quantity=quantity-#tmptb.sl from #tmptb where currentstore.bookid=#tmptb.bookid and currentstore.storename=#tmptb.storename
update currentstore set currentstore.amount=currentstore.quantity*cargo.price from cargo where cargo.cargo_id=currentstore.bookid and currentstore.bookid in(select bookid from #tmptb)
/*更新入库数量*/
insert into currentstore(id,bookid,storename)select @storemax+id,bookid,@storeno1 from #tmptb2
update currentstore set quantity=quantity+#tmptb.sl,amount=amount+#tmptb.zje from #tmptb where currentstore.bookid=#tmptb.bookid and currentstore.storename=@storeno1
end
update pcargo set shr=@shrr where orderno=@orderno
if @@error<>0
begin
rollback tran T1
end
else
begin
COMMIT TRAN T1
end
GO
相关推荐
sql server 存储过程100例
sql server 存储过程100例,说是100例,其实是超过了100例,如果看完了这些存储过程,你会发现原来存储过程如此简单.
Discuz!NT v3.0.0 SQLServer源码版
Discuz!NT3.0版本针对SQLServer2005/2008的新特性做了存储过程的全面优化,解决了以前版本存储过程因SQLServer2000语法限制造成若干存储过程无法被编译的问题,全面提升数据库运行效率。从Discuz!NT官方得知,目前新版本在官方网站的测试结果良好,showtopic等页面速度提升明显,同时服务器压力也有所降低。 在系统稳定性方面,Discuz!NT3.0的系统重构消灭了所有重复代码,同时将数据和逻辑层进行了彻底分离,避免增加功能时重复数据方法的可能。官方开发人员表示:虽然本次重构的效果短时间内不明显,但是这次重构将为以后版本增加功能打下坚实的基础! 在整体的UI和用户体验设计方面,Discuz!NT3.0在保留传统风格的同时吸取了Discuz!7.0大量被用户肯定的设计元素,形成了Discuz!NT自有的风格。首页的快速发主题可以让大论坛里面只关注某个版块的用户方便分享自己最新的事情,还有弹窗方式的快速登录、注册、发帖、编辑帖子等等快捷的功能,将有更好的用户体验效果。 另外,Discuz!NT3.0的API进行了全面开放,发帖、回复、创建用户、修改用户资料积分、创建版块、获取版块列表、获取帖子列表等功能可以让开发者很轻松的二次开发或者系统整合。3.0版本API取消了2.6版本中API许多功能受到session的限制,3.0版本API可以授权应用程序任意的修改用户密码等一系列操作,真正方便二次开发。值得一提的是,Discuz!NT3.0采用了一系列加密算法来保证安全性,Discuz!NT官方发布的API应用都可以放心使用,第三方开发的应用需要经过Discuz!NT官方认证,用户大可以放心使用。Discuz!NT3.0的API的又一大改进是支持客户端调用,基于安全考虑,官方对客户端调用程序进行了严格的功能限制。 安装包已更新,进入系统设置会提示自动打补丁
sqlserver中存储过程的递归调用示例
主要介绍了sqlserver中存储过程的递归调用示例,本文直接给出示例代码,需要的朋友可以参考下
在ORACLE里用存储过程定期分割表
Oracle数据库里存放着各种各样的数据,其中有一些数据表会随着时间的推移,越来越大。如交友聊天的日志、 短信收发的日志、生产系统的日志、动态网站发布系统的日志等等。这样的信息又和时间紧密相关,有没有办法 让这些日志表能到时间自动分割成历史年月(如log200308,log200309)的表呢? 本文将介绍用存储过程定期分割表的方法。
MySQL存储过程的优化实例
前言 在数据库的开发过程中,经常会遇到复杂的业务逻辑和对数据库的操作,这个时候就会用存储过程来封装数据库操作。如果项目的存储过程较多,书写又没有一定的规范,将会影响以后的系统维护困难和大存储过程逻辑的难以理解,另外如果数据库的数据量大或者项目对存储过程的性能要求很,就会遇到优化的问题,否则速度有可能很慢,经过亲身经验,一个经过优化过的存储过程要比一个性能差的存储过程的效率甚至高几百倍。下面介绍某一个MySQL存储过程优化的整个过程。 在本文中,需要被优化的存储过程如下: drop procedure if exists pr_dealtestnum; delimiter // create


疑难问题
