在t-sql中执行奇怪的执行时间

时间:2010-06-05 12:28:16

标签: sql-server tsql

我有两个存储过程,第一个调用第二个... 如果我单独执行第二个,则需要5分钟才能完成.. 但是当在第一个内执行时,它需要的时间不超过1分钟.. 是什么原因!

这是第一个

ALTER procedure [dbo].[schRefreshPriceListItemGroups] as

begin tran
delete from PriceListItemGroups
if @@error !=0 goto rolback
Insert PriceListItemGroups(comno,t$cuno,t$cpls,t$cpgs,t$dsca,t$cpru)
SELECT distinct c.comno,c.t$cuno, c.t$cpls,I.t$cpgs,g.t$dsca,g.t$cpru
    FROM    TTCCOM010nnn    C
    JOIN    TTDSLS032nnn    PL  ON  PL.comno    =   c.Comno     and PL.t$cpls   =   c.t$cpls
    JOIN    TTIITM001nnn    I   ON  I.t$item    =   pl.t$item   AND I.comno     =   pl.comNo
    JOIN    TTCMCS024nnn    G   ON  g.T$cprg    =   I.t$cpgs    AND g.comno     =   I.Comno
 WHERE  c.t$cpls !='' 
order by comno desc, t$cuno, t$cpgs
if @@error !=0 goto rolback
-----------------------------------------------------
Exec scrRefreshCustomersCatalogs
-----------------------------------------------------
commit tran


return
rolback:
Rollback tran

第二个

Alter proc scrRefreshCustomersCatalogs as
declare @baanIds table(id int identity(1,1),baanId varchar(12))
declare @baanId varchar(12),@i int, @n int
Insert @baanIds(BaanId)
select baanId from ftElBaanIds()
SELECT @I=1,@n=max(id) from @baanIds
select @i,@n
Begin tran
if @@error !=0 goto xRollBack
WHILE @I <=@n
    Begin
        select @baanId=baanId from @baanIds where id=@i
        if @@error !=0 goto xRollBack
        Delete from customersCatalogs where comno+'-'+t$cuno=@baanId
        print Convert(varchar,@i)+' baanId='+@baanId
        Insert customersCatalogs exec customersCatalog @baanId
        if @@error !=0 goto xRollBack

        set @i=@i+1;
    end

Commit Tran
Update statistics customersCatalogs with fullscan

Return
xRollBack:
Print '*****Rolling back*************'
Rollback tran

1 个答案:

答案 0 :(得分:1)

很难说。在第一个SP中运行时,您将进入两个事务。

我假设第二个SP与第一个SP(PriceListItemGroups)中的数据插入间接执行某些操作,因此当您自己运行第二个SP时,同样的数据是否也插入到该表中?

在第一个SP中,使用ORDER BY插入也似乎很奇怪。虽然我知道这在技术上是允许的,但几乎在所有情况下,这都是不必要的。

相关问题