为什么我的查询运行时间过长

时间:2017-03-21 20:00:10

标签: sql performance

我需要反馈为什么查询运行时间太长

With DtlProperty (PolicyNumber)

As 
(SELECT
          Archive.PolicyNumber
        FROM CLUEReporting.dbo.CLUE_Archive Archive
        Left Join CLUEReporting.dbo.PropertyAddress On (PropertyAddress.PolicyNumber = Archive.PolicyNumber)
        WHERE Archive.PolicyNumber = PropertyAddress.PolicyNumber
        AND Archive.Street       = PropertyAddress.LocAddress1
        AND Archive.Street2      = PropertyAddress.LocAddress2
        AND Archive.City         = PropertyAddress.LocCity
        AND Archive.State        = PropertyAddress.LocState
        AND Archive.Zip          = PropertyAddress.LocZipCode
),

     DtlMailing (PolicyNumber)

As 
(SELECT
          Archive.PolicyNumber
        FROM CLUEReporting.dbo.CLUE_Archive Archive
        Left Join CLUEReporting.dbo.PropertyAddress On (PropertyAddress.PolicyNumber = Archive.PolicyNumber)
        WHERE Archive.PolicyNumber = PropertyAddress.PolicyNumber
        AND Archive.Street       = PropertyAddress.bioAddress1
        AND Archive.Street2      = PropertyAddress.bioAddress2
        AND Archive.City         = PropertyAddress.bioCity
        AND Archive.State        = PropertyAddress.bioState
        AND Archive.Zip          = PropertyAddress.bioZipCode
)


--Property Address and Mailing Address
SELECT DISTINCT
  PropertyAddress.PolicyNumber                  AS PolicyNumber,
  PropertyAddress.PreviousPolicyID              AS PreviousPolicyID,
  ''                                            AS ClaimID,
  '"' + PropertyAddress.LocAddress1 + '"'       AS Street,
  '"' + PropertyAddress.LocAddress2 + '"'       AS Street2,
  '"' + PropertyAddress.LocCity + '"'           AS City,
  PropertyAddress.LocState          AS State,
  PropertyAddress.LocZipCode        AS Zip,
  'B'                               AS AddressType,
  PropertyAddress.DATABASEPOLICY    AS DATABASEPOLICY,
  PropertyAddress.ENTRYDATE         AS ENTRYDATE
FROM CLUEReporting.dbo.PropertyAddress
Left  Join DtlProperty  DtlProperty On (DtlProperty.PolicyNumber = PropertyAddress.PolicyNumber)

WHERE DtlProperty.PolicyNumber is  null 


AND PropertyAddress.PolicyNumber  IN 
        (SELECT
          PolicyNumber
        FROM CLUEReporting.dbo.PropertyAddress
        WHERE PropertyAddress.LocAddress1 = PropertyAddress.bioAddress1
        AND PropertyAddress.LocAddress2   = PropertyAddress.bioAddress2
        AND PropertyAddress.LocCity       = PropertyAddress.bioCity
        AND PropertyAddress.LocState      = PropertyAddress.bioState
        AND PropertyAddress.LocZipCode    = PropertyAddress.bioZipCode)

UNION ALL

--Property Address
SELECT DISTINCT
  PropertyAddress.PolicyNumber                  AS PolicyNumber,
  PropertyAddress.PreviousPolicyID              AS PreviousPolicyID,
  ''                                            AS ClaimID,
  '"' + PropertyAddress.LocAddress1 + '"'       AS Street,
  '"' + PropertyAddress.LocAddress2 + '"'       AS Street2,
  '"' + PropertyAddress.LocCity + '"'           AS City,
  PropertyAddress.LocState                      AS State,
  PropertyAddress.LocZipCode                    AS Zip,
  'P'                                           AS AddressType,
  PropertyAddress.DATABASEPOLICY                AS DATABASEPOLICY,
  PropertyAddress.ENTRYDATE                     AS ENTRYDATE
FROM CLUEReporting.dbo.PropertyAddress
Left  Join DtlProperty  DtlProperty On (DtlProperty.PolicyNumber = PropertyAddress.PolicyNumber)
WHERE DtlProperty.PolicyNumber is  null 


AND PropertyAddress.PolicyNumber  IN 
        (SELECT
          PolicyNumber
        FROM CLUEReporting.dbo.PropertyAddress
        WHERE (PropertyAddress.LocAddress1 != PropertyAddress.bioAddress1
        OR PropertyAddress.LocAddress2     != PropertyAddress.bioAddress2
        OR PropertyAddress.LocCity         != PropertyAddress.bioCity
        OR PropertyAddress.LocState        != PropertyAddress.bioState
        OR PropertyAddress.LocZipCode      != PropertyAddress.bioZipCode))

UNION ALL

--Mailing Address
SELECT DISTINCT
  PropertyAddress.PolicyNumber                  AS PolicyNumber,
  PropertyAddress.PreviousPolicyID              AS PreviousPolicyID,
  ''                                            AS ClaimID,
  '"' + PropertyAddress.bioAddress1 + '"'       AS Street,
  '"' + PropertyAddress.bioAddress2 + '"'       AS Street2,
  '"' + PropertyAddress.bioCity + '"'           AS City,

  PropertyAddress.bioState                      AS State,
  PropertyAddress.bioZipCode                    AS Zip,
  'M'                                           AS AddressType,
  PropertyAddress.DATABASEPOLICY                AS DATABASEPOLICY,
  PropertyAddress.ENTRYDATE                     AS ENTRYDATE
FROM CLUEReporting.dbo.PropertyAddress
Left  Join DtlMailing  DtlMailing On (DtlMailing.PolicyNumber = PropertyAddress.PolicyNumber)
WHERE DtlMailing.PolicyNumber is  null  

AND PropertyAddress.PolicyNumber  IN 
        (SELECT
          PolicyNumber
        FROM CLUEReporting.dbo.PropertyAddress
        WHERE (PropertyAddress.LocAddress1 != PropertyAddress.bioAddress1
        OR PropertyAddress.LocAddress2     != PropertyAddress.bioAddress2
        OR PropertyAddress.LocCity         != PropertyAddress.bioCity
        OR PropertyAddress.LocState        != PropertyAddress.bioState
        OR PropertyAddress.LocZipCode      != PropertyAddress.bioZipCode))

查询运行时间过长。基本上,查询检查属性地址是否在归档表上,然后确定属性地址和邮寄地址是否不相同。该查询运行超过35分钟。

1 个答案:

答案 0 :(得分:0)

这看起来并不好,尤其不是两次:

AND PropertyAddress.PolicyNumber  IN 
        (SELECT
          PolicyNumber
        FROM CLUEReporting.dbo.PropertyAddress
        WHERE (PropertyAddress.LocAddress1 != PropertyAddress.bioAddress1
        OR PropertyAddress.LocAddress2     != PropertyAddress.bioAddress2
        OR PropertyAddress.LocCity         != PropertyAddress.bioCity
        OR PropertyAddress.LocState        != PropertyAddress.bioState
        OR PropertyAddress.LocZipCode      != PropertyAddress.bioZipCode))