-1

複数のテーブルに格納されている 500000 を超える行が必要です。私のロジックですべての行を取得します。この状況で、SQL Server ブラウザーで正常に実行されるストアド プロシージャを作成しましたが、C# コードから実行すると、タイムアウト例外が発生しました。

ストアドプロシージャを改善する方法を誰か提案できますか?

CREATE PROCEDURE [dbo].[SP_ExtendedPTU]
(
    @SQLForPTU as varchar(MAX)  
)
AS
BEGIN TRAN
--DECLARE 
--@SQLForPTU as varchar(MAX)
--SET @SQLForPTU= 'WHERE  OrderID IN(4233)And OrderType =3'

BEGIN--1
DECLARE 
@ProductionTracingUnitID    as int,
@OrderID    as int,
@OrderType  as smallint,
@ProductID  as int,
@LabLabDipID    as int,
@ColorName  as varchar(255),
@PantonNo as    varchar(127),
@Shade as varchar(4),
@DyeingOrderQty as decimal(30, 17),
@JobOrderQty    as decimal(30, 17),
@ProductionPipeLineQty as decimal(30, 17),
@ProductionFinishedQty as decimal(30, 17),
@DeliveryQty    as decimal(30, 17),
@BuyerID    as int, 
@FactoryID  as int,
@ProductionGraceQty as decimal(30, 17),
@WeightLossGainQty  as decimal(30, 17),
@RateInLBS as decimal(30, 17),
@State  as smallint,
@ProductionLossQty  as decimal(30, 17),
@ReturnQty as decimal(30, 17),
@ActualDeliveryQty  as decimal(30,17),
@ReadyStockInhand as decimal(30, 17),
@JobOrderQtyApproved    as decimal(30, 17),
@OrderNumber as varchar(50),
@FactoryName as varchar(200),
@BuyerName as varchar(200),
@ProductName as varchar(200),
@YetToDelivery as decimal(30,17),
@StockInHand as decimal(30,17),
@YetToProduction as decimal(30,17),
@LCID as int,
@LCNo as varchar(300),
@PIQty as decimal(30,17),
@ChangingQty as decimal(30,17),
@SampleAdjQty as decimal(30,17),
@SampleAdjValue as decimal(30,17),
@MKTPersonID as int, 
@MKTPersonName as varchar(500),
@MerchandiserID as int,
@MerchandiserName as varchar(500),
@AmendmentStatus as smallint,
@AcceptanceValue as decimal(30,17),
@MaturityValue as decimal(30,17),
@BillAcceptanceValue as decimal(30,17),
@BillMaturityValue as decimal(30,17),
@BillAcceptancePercentage  as decimal(30,17),
@BillMaturityPercentage  as decimal(30,17),
@ExportLCValue as decimal(30,17),
@Acceptance as varchar(100),
@Maturity as varchar(100),
@YarnCount as varchar(50),
@PTUDQty  as decimal(30,17),
@ShadeFromOrder as smallint,
@EWYDLRelabNo as varchar(100),
@EWYDLColorNo as varchar(100),
@DeliveryTo as int,
@FactoryPersonnelID as int,
@BuyerPersonnelID as int,
@OrderRcvBy as int,
@OrderState as smallint

CREATE TABLE #TempTableOne(
                            ProductionTracingUnitID int,
                            OrderID int,
                            OrderType smallint,
                            ProductID int,
                            DyeingOrderQty decimal(30,17),
                            JobOrderQty decimal(30,17),
                            ProductionPipeLineQty decimal(30,17),
                            ProductionFinishedQty decimal(30,17),
                            DeliveryQty decimal(30,17),
                            BuyerID int,
                            FactoryID int,
                            ProductionGraceQty decimal(30,17),
                            WeightLossGainQty decimal(30,17),
                            RateInLBS decimal(30,17),
                            ProductionLossQty decimal(30,17),
                            ActualDeliveryQty decimal(30,17),
                            ReadyStockInhand decimal(30,17),
                            OrderNumber varchar(50),
                            FactoryName varchar(200),
                            BuyerName varchar(200),
                            ProductName varchar(200),
                            ColorName varchar(200),
                            LabLabDipID int,
                            ReturnQty decimal(30,17),
                            YetToDelivery decimal(30,17),
                            StockInHand decimal(30,17),
                            YetToProduction decimal(30,17),
                            LCID int,
                            LCNo varchar(300),
                            PIQty decimal(30,17),
                            ChangingQty decimal(30,17),
                            SampleAdjQty decimal(30,17),
                            SampleAdjValue decimal(30,17),
                            MKTPersonID int, 
                            MKTPersonName varchar(500),
                            MerchandiserID int,
                            MerchandiserName varchar(500),
                            AmendmentStatus smallint,
                            Acceptance varchar(100),
                            Maturity varchar(100),
                            YarnCount varchar(50),                          
                            EWYDLRelabNo varchar(50),
                            EWYDLColorNo varchar(50),
                            ShadeFromOrder smallint
                          )

--ProductionTracingUnitID,OrderID,OrderType,ProductID,LabLabDipID,ColorName,PantonNo,EWYDLColorNo,Shade,EWYDLRelabNo,DyeingOrderQty,JobOrderQty,ProductionPipeLineQty,ProductionFinishedQty,DeliveryQty,BuyerID,FactoryID,ProductionGraceQty,WeightLossGainQty,RateInLBS,State,ProductionLossQty,ShadeFromOrder,ReturnQty,ActualDeliveryQty,ReadyStockInhand,JobOrderQtyApproved
DECLARE 
@SQL as varchar(MAX)
SET @SQL=
'
    DECLARE Cur_AB1 CURSOR GLOBAL FORWARD_ONLY KEYSET FOR           
    SELECT ProductionTracingUnitID,OrderID,OrderType,ProductID,DyeingOrderQty,JobOrderQty,ProductionPipeLineQty,ProductionFinishedQty,BuyerID,FactoryID,ProductionGraceQty,WeightLossGainQty,RateInLBS,ProductionLossQty,ActualDeliveryQty,ReadyStockInhand,ColorName,LabLabDipID,ReturnQty,EWYDLRelabNo,EWYDLColorNo,ShadeFromOrder FROM ProductionTracingUnit '+@SQLForPTU+'
'
EXEC (@SQL)

OPEN Cur_AB1
FETCH NEXT FROM Cur_AB1 INTO @ProductionTracingUnitID,@OrderID,@OrderType,@ProductID,@DyeingOrderQty,@JobOrderQty,@ProductionPipeLineQty,@ProductionFinishedQty,@BuyerID,@FactoryID,@ProductionGraceQty,@WeightLossGainQty,@RateInLBS,@ProductionLossQty,@ActualDeliveryQty,@ReadyStockInhand,@ColorName,@LabLabDipID,@ReturnQty,@EWYDLRelabNo,@EWYDLColorNo,@ShadeFromOrder
    WHILE(@@Fetch_Status <> -1)
    BEGIN--2
        SET @LCID=0
        SET @LCNo=''
        SET @PIQty=0
        SET @AcceptanceValue =0
        SET @MaturityValue= 0
        SET @Acceptance= ''
        SET @Maturity =''


        SET @DeliveryQty=@ActualDeliveryQty-@ReturnQty
        SET @YetToDelivery=@JobOrderQty-@ActualDeliveryQty+@ReturnQty
        set @PTUDQty=(select sum(Qty) from PTUDistribution where ProductionTracingUnitID=@ProductionTracingUnitID )
        IF(@PTUDQty>@YetToDelivery)
        BEGIN--sih
            SET @StockInHand =@YetToDelivery
        END --sih
        ELSE
        BEGIN--sih2
            SET @StockInHand =@PTUDQty
        END --sih2          
        SET @YetToProduction=@JobOrderQty-@ReadyStockInhand-@ActualDeliveryQty+@ReturnQty
        IF (@YetToProduction<0)
        BEGIN
            SET @YetToProduction=0
        END

        SET @ChangingQty=0
        SET @SampleAdjQty=0
        SET @SampleAdjValue=0 
        SET @MerchandiserID=0
        SET @MerchandiserName=''
        SET @MKTPersonID =0
        SET @AmendmentStatus=0
        SET @AcceptanceValue =0
        SET @MaturityValue= 0
        SET @Acceptance= ''
        SET @Maturity =''


        SET @MKTPersonName =''
        SET @OrderNumber=''     
        IF(@OrderType=3)
        BEGIN--jam1
            SET @OrderNumber=(SELECT ISNULL(JobCode,'')+' - '+ISNULL(JobNo,'')+' / '+ISNULL(JobYear,'') FROM Job WHERE JobID=@OrderID)
            SELECT @LCID=ISNULL(ExportLCID,0), @LCNo=ISNULL(ExportLCNo,''),@AmendmentStatus=AmendmentStatus,@ExportLCValue=Amount FROM ExportLC WHERE ExportLCID =(SELECT LCID FROM [PI] WHERE PIID=(SELECT PIID FROM Job WHERE JobID=@OrderID))                    
            SELECT @PIQty=ISNULL(SUM(Qty),0), @SampleAdjQty=ISNULL(SUM(AdjQty),0), @SampleAdjValue=ISNULL(SUM(AdjValue),0) FROM PIProducts WHERE PIID=(SELECT PIID FROM Job WHERE JobID=@OrderID) AND ProductID=@ProductID          
            SET @ChangingQty=(SELECT ISNULL(SUM(Qty),0) FROM PIDeliverableProducts WHERE PIID=(SELECT PIID FROM Job WHERE JobID=@OrderID) AND ProductID=@ProductID)                 
            SELECT @MKTPersonID=EWYDLMarketingEmpID, @MerchandiserID=CmsBCPID FROM [PI] WHERE PIID=(SELECT PIID FROM Job WHERE JobID=@OrderID) 
            SET @MerchandiserName=(SELECT ISNULL([Name],'') FROM ContactPersonnel WHERE ContactPersonnelID=@MerchandiserID)         
            SET @MKTPersonName =(SELECT [Name] FROM Employee WHERE EmployeeID=@MKTPersonID)     


            SET @BillAcceptanceValue=(select isnull(sum(Amount),0) from LCbill where EXportLCID=@LCID and [state] in (2,3,4))
            SET @BillMaturityValue =(select isnull(sum(Amount),0) from LCbill where EXportLCID=@LCID and [state] in (5,6,7,8,9,10,12))

            IF(@ExportLCValue>0 and @ExportLCValue is not null)
            BEGIN
                SET @BillAcceptancePercentage  =(@BillAcceptanceValue*100)/@ExportLCValue -- bill Percentage 
                SET @BillMaturityPercentage  =(@BillMaturityValue*100)/@ExportLCValue

                SET @AcceptanceValue=(@ChangingQty*@RateInLBS)*(@BillAcceptancePercentage/100)--Percentage Wise PI Valu         
                SET @MaturityValue=(@ChangingQty*@RateInLBS)*(@BillMaturityPercentage/100)

                IF((@ChangingQty*@RateInLBS)>0 and (@ChangingQty*@RateInLBS) is not null)
                BEGIN
                    SET @AcceptanceValue=(@AcceptanceValue*100)/(@ChangingQty*@RateInLBS)-- PI ValuePercentage
                    SET @MaturityValue=(@MaturityValue*100)/(@ChangingQty*@RateInLBS)
                END

                SET @Acceptance=Convert(varchar(20),(CONVERT(float,round((@AcceptanceValue+@MaturityValue),0)))) +'%'
                SET @Maturity =Convert(varchar(20),(CONVERT(float,round(@MaturityValue,0)))) +'%'
            END

            SET @FactoryName=''
            IF(@FactoryID>0)
            BEGIN--jam3
                SET @FactoryName=(SELECT [Name] FROM Contractor WHERE ContractorID=@FactoryID)
            END--jam3

            SET @BuyerName=''
            IF(@BuyerID>0)
            BEGIN--jam4
                SET @BuyerName=(SELECT [Name] FROM Contractor WHERE ContractorID=@BuyerID)
            END--jam4

        END--jam1
        ELSE
        BEGIN --jam2 IF Sample
            SET @LCID=0
            SET @LCNo=''
            SET @OrderNumber=''
            SET @PIQty=0    
            SET @DeliveryTo=0
            SET @FactoryPersonnelID=0 
            SET @BuyerPersonnelID=0
            SET @OrderRcvBy=0
            SET @ChangingQty=0
            SET @MerchandiserName=''
            SET @MKTPersonName=''
            SET @OrderState=0
            SET @AmendmentStatus=0
            SET @PIQty= (SELECT ISNULL(SUM(Qty),0) FROM SampleOrderDetail WHERE PTUID=@ProductionTracingUnitID)
            SET @ChangingQty=@PIQty
            SELECT @OrderNumber=ISNULL(SampleOrderNo,''), @DeliveryTo=ISNULL(DeliveryTo,0),@FactoryPersonnelID=ISNULL(FactoryPersonnelID,0),@BuyerPersonnelID=ISNULL(BuyerPersonnelID,0),@OrderRcvBy=ISNULL(OrderRcvBy,0),@OrderState=ISNULL(OrderState,0)  FROM SampleOrder WHERE SampleOrderID=@OrderID
            SET @AmendmentStatus=@OrderState
            IF(@DeliveryTo=3)
            BEGIN
                SET @MerchandiserName=(SELECT ISNULL([Name],'') FROM ContactPersonnel WHERE ContactPersonnelID=@FactoryPersonnelID)     
            END

            IF(@DeliveryTo=2)
            BEGIN
                SET @MerchandiserName=(SELECT ISNULL([Name],'') FROM ContactPersonnel WHERE ContactPersonnelID=@BuyerPersonnelID)
            END
            SET @MKTPersonName =(SELECT [Name] FROM Employee WHERE EmployeeID=@OrderRcvBy)      

            IF(@DeliveryTo=3)
            BEGIN
                SET @FactoryName=(SELECT [Name] FROM Contractor WHERE ContractorID=@FactoryID)
            END

            IF(@DeliveryTo=2)
            BEGIN
                SET @FactoryName=(SELECT [Name] FROM Contractor WHERE ContractorID=@BuyerID)
            END     

            SET @BuyerName=''
            IF(@BuyerID>0)
            BEGIN--jam4
                SET @BuyerName=(SELECT [Name] FROM Contractor WHERE ContractorID=@BuyerID)
            END--jam4
        END--jam2

        SET @ProductName=''
        SET @YarnCount =''
        SELECT @ProductName=('['+ Code+ '] '+ [Name]), @YarnCount =[Count]  FROM Yarncategory WHERE YarncategoryID=@ProductID       

        INSERT INTO #TempTableOne Values(ISNULL(@ProductionTracingUnitID,0),ISNULL(@OrderID,0),ISNULL(@OrderType,0),ISNULL(@ProductID,0),ISNULL(@DyeingOrderQty,0),ISNULL(@JobOrderQty,0),ISNULL(@ProductionPipeLineQty,0),ISNULL(@ProductionFinishedQty,0),ISNULL(@DeliveryQty,0),ISNULL(@BuyerID,0),ISNULL(@FactoryID,0),ISNULL(@ProductionGraceQty,0),ISNULL(@WeightLossGainQty,0),ISNULL(CONVERT (decimal(18,2),@RateInLBS),0),ISNULL(@ProductionLossQty,0),ISNULL(@ActualDeliveryQty,0),ISNULL(@ReadyStockInhand,0),ISNULL(@OrderNumber,''),ISNULL(@FactoryName,''),ISNULL(@BuyerName,''),ISNULL(@ProductName,''),ISNULL(@ColorName,''),ISNULL(@LabLabDipID,0),ISNULL(@ReturnQty,0),ISNULL(@YetToDelivery,0),ISNULL(@StockInHand,0),ISNULL(@YetToProduction,0),ISNULL(@LCID,0),ISNULL(@LCNo,''),ISNULL(@PIQty,0),ISNULL(@ChangingQty,0),ISNULL(@SampleAdjQty,0),ISNULL(@SampleAdjValue,0),ISNULL(@MKTPersonID,0),ISNULL(@MKTPersonName,''),ISNULL(@MerchandiserID,0),ISNULL(@MerchandiserName,''),ISNULL(@AmendmentStatus,0),ISNULL(@Acceptance,''),ISNULL(@Maturity,''),ISNULL(@YarnCount,''),ISNULL(@EWYDLRelabNo,''),ISNULL(@EWYDLColorNo,''),ISNULL(@ShadeFromOrder,0))
FETCH NEXT FROM Cur_AB1 INTO @ProductionTracingUnitID,@OrderID,@OrderType,@ProductID,@DyeingOrderQty,@JobOrderQty,@ProductionPipeLineQty,@ProductionFinishedQty,@BuyerID,@FactoryID,@ProductionGraceQty,@WeightLossGainQty,@RateInLBS,@ProductionLossQty,@ActualDeliveryQty,@ReadyStockInhand,@ColorName,@LabLabDipID,@ReturnQty,@EWYDLRelabNo,@EWYDLColorNo,@ShadeFromOrder
END--2
CLOSE Cur_AB1
DEALLOCATE Cur_AB1
SELECT * FROM #TempTableOne Order By OrderID
--Group By Product
SELECT ProductID,ProductName, YarnCount,  SUM(PIQty) as PIQty, SUM(ChangingQty) AS ChangingQty, SUM(SampleAdjQty) AS SampleAdjQty, SUM(SampleAdjValue) as SampleAdjValue, SUM(DyeingOrderQty) as DyeingOrderQty,SUM(JobOrderQty)AS JobOrderQty,SUM(ProductionPipeLineQty)as ProductionPipeLineQty,SUM(ProductionFinishedQty) as ProductionFinishedQty,SUM(DeliveryQty)as DeliveryQty,SUM(ProductionGraceQty)AS ProductionGraceQty,SUM(WeightLossGainQty) as WeightLossGainQty,SUM(ProductionLossQty)as ProductionLossQty,SUM(ActualDeliveryQty)as ActualDeliveryQty,SUM(ReadyStockInhand)as ReadyStockInhand, SUM(ReturnQty) AS ReturnQty,SUM(YetToDelivery)AS YetToDelivery,SUM(StockInHand)AS StockInHand,SUM(YetToProduction)AS YetToProduction  FROM #TempTableOne GROUP BY ProductID,ProductName,YarnCount Order By ProductID
--Group By Factory
SELECT FactoryID,FactoryName,SUM(PIQty) as PIQty, SUM(ChangingQty) AS ChangingQty, SUM(SampleAdjQty) AS SampleAdjQty, SUM(SampleAdjValue) as SampleAdjValue, SUM(DyeingOrderQty) as DyeingOrderQty,SUM(JobOrderQty)AS JobOrderQty,SUM(ProductionPipeLineQty)as ProductionPipeLineQty,SUM(ProductionFinishedQty) as ProductionFinishedQty,SUM(DeliveryQty)as DeliveryQty,SUM(ProductionGraceQty)AS ProductionGraceQty,SUM(WeightLossGainQty) as WeightLossGainQty,SUM(ProductionLossQty)as ProductionLossQty,SUM(ActualDeliveryQty)as ActualDeliveryQty,SUM(ReadyStockInhand)as ReadyStockInhand, SUM(ReturnQty) AS ReturnQty,SUM(YetToDelivery)AS YetToDelivery,SUM(StockInHand)AS StockInHand,SUM(YetToProduction)AS YetToProduction FROM #TempTableOne GROUP BY FactoryID,FactoryName
--Group By Order    
SELECT OrderID,OrderNumber,LCNo,FactoryID,FactoryName,BuyerID,BuyerName,SUM(PIQty) AS PIQty,SUM(ChangingQty) AS ChangingQty,SUM(SampleAdjQty) AS SampleAdjQty,SUM(SampleAdjValue) AS SampleAdjValue,MKTPersonID,MKTPersonName,MerchandiserID,MerchandiserName,AmendmentStatus,Acceptance,Maturity,SUM(DyeingOrderQty) as DyeingOrderQty,SUM(JobOrderQty)AS JobOrderQty,SUM(ProductionPipeLineQty)as ProductionPipeLineQty,SUM(ProductionFinishedQty) as ProductionFinishedQty,SUM(DeliveryQty)as DeliveryQty,SUM(ProductionGraceQty)AS ProductionGraceQty,SUM(WeightLossGainQty) as WeightLossGainQty,SUM(ProductionLossQty)as ProductionLossQty,SUM(ActualDeliveryQty)as ActualDeliveryQty,SUM(ReadyStockInhand)as ReadyStockInhand, SUM(ReturnQty) AS ReturnQty,SUM(YetToDelivery)AS YetToDelivery,SUM(StockInHand)AS StockInHand,SUM(YetToProduction)AS YetToProduction FROM #TempTableOne GROUP BY OrderID,OrderNumber,OrderNumber,LCNo,FactoryID,FactoryName,BuyerID,BuyerName,MKTPersonID,MKTPersonName,MerchandiserID,MerchandiserName,AmendmentStatus,Acceptance,Maturity
--Default View
SELECT OrderID,OrderNumber,LCNo,FactoryID,FactoryName,BuyerID,BuyerName,ProductID,ProductName,YarnCount,PIQty,ChangingQty,SampleAdjQty,SampleAdjValue,RateInLBS,MKTPersonID,MKTPersonName,MerchandiserID,MerchandiserName,AmendmentStatus,Acceptance,Maturity, SUM(DyeingOrderQty) as DyeingOrderQty,SUM(JobOrderQty)AS JobOrderQty,SUM(ProductionPipeLineQty)as ProductionPipeLineQty,SUM(ProductionFinishedQty) as ProductionFinishedQty,SUM(DeliveryQty)as DeliveryQty,SUM(ProductionGraceQty)AS ProductionGraceQty,SUM(WeightLossGainQty) as WeightLossGainQty,SUM(ProductionLossQty)as ProductionLossQty,SUM(ActualDeliveryQty)as ActualDeliveryQty,SUM(ReadyStockInhand)as ReadyStockInhand, SUM(ReturnQty) AS ReturnQty,SUM(YetToDelivery)AS YetToDelivery,SUM(StockInHand)AS StockInHand,SUM(YetToProduction)AS YetToProduction  FROM #TempTableOne GROUP BY OrderID,OrderNumber,LCNo,FactoryID,FactoryName,BuyerID,BuyerName,ProductID,ProductName,YarnCount,PIQty,ChangingQty,SampleAdjQty,SampleAdjValue,RateInLBS,MKTPersonID,MKTPersonName,MerchandiserID,MerchandiserName,AmendmentStatus,Acceptance,Maturity   
DROP TABLE #TempTableOne
END--1
COMMIT TRAN
4

3 に答える 3

1

ここには多くの問題があります。インデックスにもよりますが、適切に記述すれば、この手順は約 15 秒で問題なく実行できます。詳細を掘り下げる前に、私があなたに与えることができるヒントは次のとおりです。

まず、一括処理を行うときにカーソルを使用できるのは、一括処理で更新を分割する場合のみです。つまり、すべてのレコードを更新するために反復処理が必要なカーソルは使用しないでください。手順全体を 500,000 回反復する必要があり、非常に遅くなります。むしろ、一時テーブルを使用してデータを挿入し、そのテーブルを使用して更新を実行します。以下のコードに示すように、一時インデックスを使用して一時テーブルにインデックスを付けることができます。一時テーブルとライブ テーブルの両方で、WHERE 句で使用されるすべてのフィールドがインデックス化されていることを確認してください。

CREATE TABLE #Temp2 (
    ProductionTracingUnitID int,
    OrderID int,
    OrderType smallint,
    ProductID int,
    DyeingOrderQty  decimal(30, 17),
    JobOrderQty     decimal(30, 17),
    ProductionPipeLineQty  decimal(30, 17),
    ProductionFinishedQty  decimal(30, 17),
    BuyerID     int, 
    FactoryID   int,
    ProductionGraceQty  decimal(30, 17),
    WeightLossGainQty   decimal(30, 17),
    RateInLBS  decimal(30, 17),
    ProductionLossQty   decimal(30, 17),
    ActualDeliveryQty   decimal(30,17),
    ReadyStockInhand  decimal(30, 17),
    ColorName   varchar(255),
    LabLabDipID     int,
    ReturnQty  decimal(30, 17),
    EWYDLRelabNo  varchar(100),
    EWYDLColorNo  varchar(100),
    ShadeFromOrder  smallint
)
CREATE NONCLUSTERED INDEX #IX_Temp2_1 ON #Temp2(ProductionTracingUnitID)
CREATE NONCLUSTERED INDEX #IX_Temp2_2 ON #Temp2(OrderID)
CREATE NONCLUSTERED INDEX #IX_Temp2_3 ON #Temp2(ProductID)

DECLARE @SQL as varchar(MAX)
SET @SQL=
'
    SELECT ProductionTracingUnitID,OrderID,OrderType,ProductID,DyeingOrderQty,JobOrderQty,ProductionPipeLineQty,ProductionFinishedQty,BuyerID,FactoryID,ProductionGraceQty,
        WeightLossGainQty,RateInLBS,ProductionLossQty,ActualDeliveryQty,ReadyStockInhand,ColorName,LabLabDipID,ReturnQty,EWYDLRelabNo,EWYDLColorNo,ShadeFromOrder 
    FROM ProductionTracingUnit '+@SQLForPTU
INSERT INTO #Temp2 (ProductionTracingUnitID, OrderID, OrderType, ProductID, DyeingOrderQty, JobOrderQty, ProductionPipeLineQty, 
        ProductionFinishedQty, BuyerID, FactoryID, ProductionGraceQty, WeightLossGainQty, RateInLBS, ProductionLossQty, ActualDeliveryQty,
        ReadyStockInhand, ColorName, LabLabDipID, ReturnQty, EWYDLRelabNo, EWYDLColorNo, ShadeFromOrder)
EXEC (@SQL)

次に、一括処理を行う場合、行ごとに実行する必要があるサブクエリの使用を避けます。次の行は、最良の場合でも非常に遅くなります。

SELECT @LCID=ISNULL(ExportLCID,0), @LCNo=ISNULL(ExportLCNo,''),@AmendmentStatus=AmendmentStatus,@ExportLCValue=Amount 
FROM ExportLC WHERE ExportLCID =(SELECT LCID FROM [PI] WHERE PIID=(SELECT PIID FROM Job WHERE JobID=@OrderID))                    

次のように、結合で言い換えることを検討してください。

SELECT @LCID=ISNULL(ExportLCID,0), @LCNo=ISNULL(ExportLCNo,''),@AmendmentStatus=AmendmentStatus,@ExportLCValue=Amount 
FROM ExportLC e, [PI] p, Job j
WHERE e.ExportLCID = p.LCID
AND p.PIID = j.PIID
AND j.JobID = @OrderID

カーソルを使用せずに手順を書き直すと、次のようになります。

UPDATE #Temp2 SET LCID = ISNULL(ExportLCID, 0), LCNo = ISNULL(ExportLCNo, 0), ...
FROM #Temp2 t, ExportLC e, [PI] p, Job j
WHERE e.ExportLCID = p.LCID
AND p.PIID = j.PIID
AND j.JobID = t.OrderID

これをすべて機能させるにはかなりの作業が必要であり、そのような複合プロセス用のテスト環境を作成するのは困難です。ただし、これで続行するのに十分なはずだと思います。行き詰まったらお知らせください。

于 2012-05-02T09:36:09.547 に答える
0

500,000 行を解析している場合、カーソルはスターターではありません。これはあらゆる種類の問題を引き起こし、データが大きくなるにつれて、データはますます遅くなります。パフォーマンスを向上させる方法はいくつかあります。

1) 可能であれば、一晩でデータをフェッチします。つまり、SQL ジョブを使用してデータをスナップショット テーブルにフラット化します。

2) バッチ選択でできるだけ多くのデータをフェッチします。つまり、一時テーブルにループ内の行ごとに挿入するのではなく、クエリを実行して、特定の列に必要なすべてのデータを取得できます。

3) これを一晩で行うことができず、常にこのように行う必要がある場合は、これを目立たないストアド プロシージャに分割し、C# コード内からループすることをお勧めします。そうすれば、おそらく進行状況バーの形で、ユーザーにフィードバックを提供できます。これはタイムアウトしません。また、ユーザーが何かが計算されていることを認識していれば、どれだけ時間がかかるかは問題ではありません。ユーザーは、待機する必要がある時間を大まかに把握しており、をしたい。

于 2012-05-02T07:08:23.613 に答える
0

SQL Server の実行計画を使用して、ボトルネックを特定できます。また、可能であればカーソルの使用を避けてください。また、一時テーブルに大量のデータが含まれている場合は、#table を作成する代わりに通常のテーブルを作成してください。

于 2012-05-02T06:06:44.403 に答える