日期:2014-05-17  浏览次数:20576 次

sql2005存储过程执行的时间问题
   我做了一个存储过程,作用是根据将已经生产的货物,按照制单上面走货先后顺序,详细到每一个货期、颜色、尺码来推数。
   比如,第一、二货期都有红色中码,一个是2000,一个是3000,已经生产了红色中码2500,那么就会先满足第一货期的红色中码,然后剩下500就给第二货期。如果生产了5500,那么多出的数量全部放在最后一个货期。
   因为代码太长了,所以不放出来,我遇上的问题就是有时候执行的时间是13分钟,有时候是5分钟,有时候是9分钟......如果是上班时间执行,时间大都很少,只有深夜执行才会有13分钟,但是有时候也会不到这么多时间。
   我想请问,抛开系统、硬件、病毒这些原因,到底还有什么会导致执行时间不足的?因为上班的时候,数据时即是产生,这个时候执行的时间特别少,结果自然很多数据都推不上了。

------解决方案--------------------
--说明:TempTable这个表,自己先创建一下
--Insert into #ee (PO,ShipDate,Color,Sizx,Qty,ProcessID,CuttingQty,Qtyx) 这好几个语句可以合成一个,想办法
--不要用游标,试着去SQL实现
--总之,这个存储过程写的非常不好。

ALTER  Procedure [dbo].[ShipDateQtyPush_CuttingQty] @PO varchar(12), @ShipDate varchar(10)
as
declare @sql        varchar(8000)
if exists (select * from tempdb.dbo.sysobjects  where  name='#bb' ) drop table #bb
if exists (select * from tempdb.dbo.sysobjects  where  name='##cc' ) drop table ##cc
if exists (select * from tempdb.dbo.sysobjects  where  name='##dd' ) drop table ##dd
if exists (select * from tempdb.dbo.sysobjects  where  name='#gg' ) drop table #gg
--
Create Table #ProcessQty
(
    id            [int] identity (1,1) not null,   --10
    PO            [varchar] (20),
    Color        [varchar] (50),
    Sizx        [varchar] (20),
    Qty            int,
    ProcessID    [varchar] (3)
)
DECLARE @begin DATETIME
SET @begin = GETDATE()

Insert into #ProcessQty (PO,ProcessID,Color,Sizx,Qty)
select
    Left(jo_master.cust_so_ref,CharIndex('-',jo_master.cust_so_ref)-1)  PONO,[fg_rfid]..rfid_transaction_table.major,