通过存储过程将数据从一个数据库复制到另一个数据库需要很长时间

时间:2015-10-27 12:01:37

标签: sql-server tsql stored-procedures

编写存储过程以从一个数据库复制数据并插入到不同的数据库中。但目前这些程序需要花费大量时间才能完成。为什么会发生这种情况以及如何减少程序的运行时间?

我的代码:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [PPA].[SP_LOAD_PR_FROM_PEOPLESOFT]
    @success        BIT OUTPUT
AS
    --//Declaring the variables
    DECLARE @MessageDescription         VARCHAR(200)
    DECLARE @Message                    VARCHAR(1000)
    DECLARE @REQ_ID                     VARCHAR(10)
    DECLARE @BUSINESS_UNIT              CHAR(5)
    DECLARE @CURRENCY_CD                CHAR(3)
    DECLARE @CNT_REQUESTS               NUMERIC
    DECLARE @REVISION_NUMBER            NUMERIC(18,0)
    DECLARE @PS_DATE_MODIFIED           DATETIME
    DECLARE @PPA_DATE_MODIFIED          DATETIME            
    DECLARE @REQUEST_STATUS_CODE        NUMERIC(18,0)
    DECLARE @REQUEST_ID                 NUMERIC(38,0)
    DECLARE @PS_DATE_CREATED            DATETIME

BEGIN

SET @success = 0
SET @MessageDescription = 'Stored procedure SP_LOAD_PR_FROM_PEOPLESOFT '
PRINT 'Inside ' + @MessageDescription

--//DECLARE the CURSOR to retrieve approved PRs from PeopleSoft's table PS_REQ_HDR
DECLARE cursor_ps_req_hdr CURSOR READ_ONLY FOR
SELECT
    BUSINESS_UNIT,
    LTRIM(RTRIM(REQ_ID)) AS REQ_ID,
    CURRENCY_CD,
    LAST_DTTM_UPDATE,   ----  get PR UPDATION date time :to address issue C_42
    REQ_DT          ----  get PR CREATION date time :to address issue C_42
FROM    PPA_PS_DAILY.PPA.PS_REQ_HDR
WHERE   REQ_STATUS  IN ('P', 'A')
    AND HOLD_STATUS = 'N'
    AND BUSINESS_UNIT IN ('GLPO1', 'ILPO1', 'INPO1', 'SOPO1', 'SSPO1' ,'TSPO1','USPO1','AUPO1','CNPO1','FRPO1','SCPO1','UKPO2','SIPO1')
ORDER BY BUSINESS_UNIT, REQ_ID

OPEN cursor_ps_req_hdr

------------------------------------------
PRINT 'Count of rows before fetching in cursor cursor_ps_req_hdr : '+cast(@@cursor_rows as varchar)
------------------------------------------

FETCH NEXT FROM cursor_ps_req_hdr INTO
@BUSINESS_UNIT,
@REQ_ID,
@CURRENCY_CD,
@PS_DATE_MODIFIED,  -- to address issue C_42
@PS_DATE_CREATED    -- to address issue C_42

--//Check @@FETCH_STATUS to see if there are any records to retrieve.
IF @@FETCH_STATUS <> 0
BEGIN
    --//No approved PRs exist in table PS_REQ_HDR
    SET @Message = @MessageDescription + ': No approved PRs to retrieve from table PS_REQ_HDR.'
    PRINT @Message
    --//log message in a log file

END

WHILE @@FETCH_STATUS = 0
BEGIN       
    --Check if retrieved PR has associated request in PPA
    SELECT @CNT_REQUESTS = COUNT(*) 
    FROM PPA.REQUEST REQ 
    WHERE REQ.PR_NUMBER = @REQ_ID 
    AND REQ.BUYER_COMPANY_ID = @BUSINESS_UNIT

    IF @CNT_REQUESTS = 0 --If no associated request exists for a PR
    BEGIN
        SET @REVISION_NUMBER = 0        -- to address issue C_42

        --insert new request and its line items into PPA back end
        EXEC PPA.SP_INSERT_REQUEST @REQ_ID, @BUSINESS_UNIT, @CURRENCY_CD,@REVISION_NUMBER,@PS_DATE_CREATED,@PS_DATE_MODIFIED,@success --Added @REVISION_NUMBER,@PS_DATE_CREATED,@PS_DATE_MODIFIED :to address issue C_42
        IF @success = 0 
        BEGIN
            SET @Message = @MessageDescription + ': Failed to INSERT request and/or its line items into PPA back-end for REQ_ID = ' + @REQ_ID + ' BUSINESS_UNIT = ' + @BUSINESS_UNIT
        END
    END
    ELSE    --If associated request exists for a PR in PPA
    BEGIN
-----------------------------------------------addressing issue C_42: Start---------------------------------------------------------------------            
        --Check request status and find 

PS_DATE_MODIFIED,REQUEST_ID,REVISION_NUMBER for the request at PPA end    
            SELECT TOP 1 @REQUEST_ID=REQ.REQUEST_ID,@REQUEST_STATUS_CODE=REQ.REQUEST_STATUS_CODE,
                @REVISION_NUMBER = REQ.REVISION_NUMBER,@PPA_DATE_MODIFIED = REQ.PS_DATE_MODIFIED
            FROM PPA.REQUEST REQ
            WHERE REQ.PR_NUMBER = @REQ_ID 
            AND REQ.BUYER_COMPANY_ID = @BUSINESS_UNIT 
            ORDER BY REQ.REVISION_NUMBER DESC,REQ.DATE_CREATED DESC

        --Check if there is any difference in modified dates at PS and PPA end
        IF @PS_DATE_MODIFIED!=@PPA_DATE_MODIFIED
        BEGIN           

            SET @Message = @MessageDescription + ' : Request status code for REQUEST_ID '+CAST(@REQUEST_ID AS VARCHAR)+' REQ_ID = ' + @REQ_ID + ' BUSINESS_UNIT = ' + @BUSINESS_UNIT+' is '+CAST(@REQUEST_STATUS_CODE AS VARCHAR)
            PRINT @Message

            --Check if request at PPA end has request status code other than 10(incomplete)
            IF @REQUEST_STATUS_CODE <> 10
            BEGIN
                SET @REVISION_NUMBER = @REVISION_NUMBER+1
                --insert new request and its line items into PPA back end with next revision number
                EXEC PPA.SP_INSERT_REQUEST @REQ_ID, @BUSINESS_UNIT, @CURRENCY_CD,@REVISION_NUMBER,@PS_DATE_CREATED,@PS_DATE_MODIFIED,@success --Added @REVISION_NUMBER,@PS_DATE_CREATED,@PS_DATE_MODIFIED :to address issue C_42
                IF @success = 0 
                BEGIN
                    SET @Message = @MessageDescription + ': Failed to INSERT request and/or its line items into PPA back-end for REQ_ID = ' + @REQ_ID + ' BUSINESS_UNIT = ' + @BUSINESS_UNIT
                    PRINT @Message
                END
            END
            ELSE        -- Process the PR if request_status is incomplete i.e.(request_status_code=10) 
            BEGIN
                --Update the PR into PPA back end 
                EXEC PPA.SP_UPDATE_ITEMS_OF_PR @REQ_ID, @BUSINESS_UNIT, @CURRENCY_CD,@REVISION_NUMBER,@REQUEST_ID,@PS_DATE_MODIFIED,@PS_DATE_CREATED, @success
                IF @success = 0 
                BEGIN
                    SET @Message = @MessageDescription + ': Failed to UPDATE request into PPA back-end for PR_NUMBER = ' + @REQ_ID + ' BUSINESS_UNIT = ' + @BUSINESS_UNIT
                    PRINT @Message
                END
            END
        END 
        -- Updated by Kunal, Calling to update PR items regardless of the state of the request
        if(select count(*) from PPA.REQ_LINE_ITEM_PRODUCT rli,PPA.REQUEST req where rli.REQUEST_ID=req.REQUEST_ID and req.PR_NUMBER=@REQ_ID and req.BUYER_COMPANY_ID=@BUSINESS_UNIT and rli.DELETION_MARK=1)>0
            begin
                print 'reached to Kunals call for the proc update items'
                EXEC PPA.SP_UPDATE_ITEMS_OF_PR @REQ_ID, @BUSINESS_UNIT, @CURRENCY_CD,@REVISION_NUMBER,@REQUEST_ID,@PS_DATE_MODIFIED,@PS_DATE_CREATED, @success
            end
    END

-----------------------------------------------addressing issue C_42:End-------------------------------------------------------------------------

    --Retrieve comments from PeopleSoft and attempt inserting them into PPA's tables
    SET @success = 1
    EXEC PPA.SP_INSERT_COMMENTS @REQ_ID, @BUSINESS_UNIT, @success
    IF @success = 0 
    BEGIN
        SET @Message = @MessageDescription + ': Failed to INSERT comments into PPA back-end for PR_NUMBER = ' + @REQ_ID + ' BUSINESS_UNIT = ' + @BUSINESS_UNIT
        PRINT @Message
    END

    FETCH NEXT FROM cursor_ps_req_hdr INTO
    @BUSINESS_UNIT,
    @REQ_ID,
    @CURRENCY_CD,
    @PS_DATE_MODIFIED,  -- to address issue C_42
    @PS_DATE_CREATED    -- to address issue C_42
END

--//Close and Deallocate the cursor
CLOSE cursor_ps_req_hdr
DEALLOCATE cursor_ps_req_hdr
PRINT @MessageDescription + ': Closed and Deallocated CURSOR cursor_ps_req_hdr'

SET @success = 1
PRINT   'Successfully exiting ' + @MessageDescription
RETURN  @success

ERRORHANDLER:
    PRINT @MessageDescription + ': Inside ERRORHANDLER'

IF CURSOR_STATUS('global', 'cursor_ps_req_hdr') >= 0
BEGIN
    CLOSE cursor_ps_req_hdr
    DEALLOCATE cursor_ps_req_hdr
    PRINT @MessageDescription + ': Closed and Deallocated CURSOR cursor_ps_req_hdr'
END

SET @success = 0
--//log the message in a log file if @MessageDesc <> NULL
PRINT   'Exiting ERRORHANDLER of ' + @MessageDescription
RETURN @success
END

1 个答案:

答案 0 :(得分:0)

如何改善表现:

  1. 移除光标&amp; RBAR(按行划分)样式编码
  2. 如果无法删除光标,请至少将其更改为fast_forward和local
  3. 有5个不同程序的电话。没有关于它们的任何细节,它们可能是原因,可能需要调整
  4. 查看执行计划&amp;统计信息输出。专注于具有最高IO和扫描的操作,导致大量IO,昂贵的排序,假脱机,大量执行的密钥查找
  5. 删除打印件
  6. 让更有经验的人帮助您