SQL Server存储过程超时

时间:2016-03-02 19:12:37

标签: sql-server stored-procedures

以下存储过程需要大约2分钟才能运行,这会导致超时。两个表都确实定义了主键和索引。无论如何我可以改善处理时间吗?

ALTER PROCEDURE [dbo].[dfc_rpt_validation]
 -- declare
    @region varchar(10) = null,
    @source varchar(10) = null
AS BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
select row_number() OVER(ORDER BY l.loan_code) as Id
    , l.region AS Region
    , l.source AS Source
    , l.loan_code_string AS CL_Loan_Code
    , l.loan_principal_amt AS CL_Loan_Principal_Amt
    , l.loan_amt_due AS CL_Loan_Amt_Due
    , l.date_originated AS CL_Origination_Date
    , l.StoreNumber AS CL_Store_Number

    , v.new_loan_code_string  AS FA_Loan_Code
    , v.loan_principal_amt AS FA_Loan_Principal_Amt
    , v.loan_amt_due AS FA_Loan_Amt_Due
    , v.loan_origination_date AS FA_Origination_Date

    , (select count(*) from [dbo].[dfc_LoanValidatedRecords] s WHERE s.loan_code_string = l.loan_code_string) AS FA_Times_Loan_Document_Processed

    , (CASE WHEN l.rejected_date IS NULL AND l.validated_date IS NULL THEN ''
        WHEN v.pdf_legible = 0 THEN 'operational reject' -- operational error
        WHEN v.loan_code_valid = 1
            AND v.loan_principal_amt_valid = 1
            AND v.loan_origination_date_valid = 1
            AND v.loan_amt_due_valid = 1
            --OR v.pdf_legible = 0  -- operational error
        THEN 'validated' ELSE 'rejected' END
        ) AS [FA_Verification_Status]
            -- 100 delivery method failure
            -- 200 pdf reject codes
            -- 400 borrower info reject codes
            -- 600 loan reject codes

            ,  LTRIM(RTRIM (
              --(CASE WHEN l.rejected_date IS NULL AND l.validated_date IS NULL THEN ''
                --ELSE
                (CASE WHEN v.pdf_legible                   = 0 THEN ' 200'
                 ELSE
                 (CASE WHEN v.loan_code_valid             = 0 THEN ' 600' ELSE '' END)
                + (CASE WHEN v.loan_principal_amt_valid    = 0 THEN ' 610' ELSE '' END)
                + (CASE WHEN v.loan_origination_date_valid = 0 THEN ' 620' ELSE '' END) -- LoanDate
                + (CASE WHEN v.loan_amt_due_valid          = 0 THEN ' 625' ELSE '' END)
                END)  -- operational error
              --END)
            )) AS FA_Reason

            , l.load_file AS load_file

from dfc_LoanRecords AS l
JOIN dfc_LoanValidatedRecords AS v ON v.loan_code_string = l.loan_code_string
WHERE CONVERT (DATE, l.load_date) >= convert(date, dateadd(hh,-8,getdate())) -- handle UTC conversion
        AND l.region = @region AND l.source = @source
ORDER BY FA_Verification_Status, FA_Reason
END

2 个答案:

答案 0 :(得分:1)

最简单的改变我可以看到: 我没理由看到这个:CONVERT(DATE,l.load_date)。 只需使用l.load_date> = convert(date,dateadd(hh,-8,getdate()))

这应该使SQL查询优化器能够使用load_date上可能存在的任何索引。目前,它不能有效地使用列上的功能。

答案 1 :(得分:0)

如果不转换Convert(date, dateadd(hh,-8,getdate())),那就不会更好      在查询本身,以避免每个记录的功能

DECLARE @Date DATE = CAST(DATEADD(HH,-8,GETDATE()) AS DATE)

WHERE CONVERT (DATE, l.load_date) >=  @Date -- handle UTC conversion
    AND l.region = @region AND l.source = @source

通过这种方式,您将获得更好的执行计划,而不是转换每一行的值。

此外,如果你可以在application layer... please read 7 Things Developers Should Know About SQL Server item 2申请ORDER BY,那么除非我们不得不在数据库方面使用它,否则它将减少tempdb上的额外费用。 / p>