2013-05-13 228 views
0

您好所有下面都是我爲SQL 2008編寫的查詢。插入500000條記錄需要2個多小時。任何人都可以提出一種提高性能的方法嗎?SQL查詢執行時間太長

INSERT INTO tblUserFile 
SELECT 
    CASE 
     WHEN UD.IdentityStatus = 'A' THEN 'ACTIVE' 
     WHEN UD.IdentityStatus in ('T','') THEN 'INACTIVE' 
     WHEN UD.IdentityStatus IS NULL THEN '' 
    END, 
    --'UD.IS' AS "Status", 
    ISNULL(UD.HltID,'') AS "USERID", 
    ISNULL(UD.HltID,'') AS "USERNAME", 
    ISNULL(UD.FirstName,'') AS "FIRSTNAME", 
    ISNULL(UD.LastName,'') AS "LASTNAME", 
    ISNULL(UD.MiddleInitials,'') AS "MI", 
    '' AS "GENDER", 
    ISNULL(UD.EmailAddress,'') AS "EMAIL", 
    CASE 
     WHEN SU.UserType = 'C' THEN ISNULL(MCU.Manager, '') ----look into this 
     WHEN SU.UserType = 'R' THEN 'From LMS SuperViser' 
     WHEN SU.UserType IS NULL OR SU.UserType = '' THEN '' 
    END, 
    '' AS HR, 
    '' AS "DEPARTMENT", 
    '' AS "JOBCODE", 
    '' AS "DIVISION", 
    ISNULL(UD.Office,'') AS "LOCATION", 
    '' AS "TIMEZONE", 
    '' AS "HIREDATE", 
    ISNULL(UD.Title,'') AS "TITLE", 
    ISNULL(UD.StreetAddress,'') AS "ADDR1", 
    '' AS "ADDR2", 
    ISNULL(UD.City,'') AS "CITY", 
    ISNULL(UD.State,'') AS "STATE", 
    ISNULL(UD.Zip,'') AS "ZIP", 
    ISNULL(UD.CountryCode,'') AS "COUNTRY", 
    '' AS "REVIEW_FREQ", 
    '' AS "LAST_REVIEW_DATE", 
    ISNULL(UD.EmployeeType,'') AS "Custom01", 
    '' AS "Custom02", 
    CASE 
     WHEN SU.UserType = 'C' THEN '' 
     WHEN SU.UserType = 'R' THEN ISNULL(FSBD.Name,'') 
     WHEN SU.UserType IS NULL OR SU.UserType = '' THEN '' 
    END, 
    '' AS "Custom04", 
    '' AS "Custom05", 
    '' AS "Custom06", 
    '' AS "Custom07", 
    '' AS "Custom08", 
    CASE 
     WHEN SU.UserType = 'C' THEN 'Corporate' 
     WHEN SU.UserType = 'R' THEN 'Hotel' 
     WHEN SU.UserType IS NULL OR SU.UserType = '' THEN '' 
    END, 
    ISNULL(UD.EmpId,'') AS "Custom11", 
    '' AS "Custom13", 
    '' AS "Custom14", 
    '' AS "Custom15", 
    '' AS "PositionCode", 
    ISNULL(SU.HomeFacility, '') AS "HomeFacility", 
    'NPS' AS PSFlag 
FROM Search..UserData UD 
    LEFT JOIN Search..ManagerForCorpUsers MCU ------ look into this 
    ON MCU.EmpID = UD.EmpId 
    AND UD.EmpId != '' 
    AND UD.EmpId IS NOT NULL 
    LEFT JOIN Search..securityUsers SU ------ look into this 
    ON UD.UserId = SU.UserID 
    AND UD.UserId != '' 
    AND UD.UserId IS NOT NULL 
    LEFT JOIN EIS.dbo.NewQueryFilter NQ 
    ON SU.HomeFacility = NQ.FCNB 
    AND SU.HomeFacility != '' 
    AND SU.HomeFacility IS NOT NULL 
    LEFT JOIN Facility..fcSubBrandDesc FSBD 
    ON NQ.SubBrand = FSBD.SubBrand 
    AND NQ.SubBrand != '' 
    AND NQ.SubBrand IS NOT NULL 
WHERE 
    ISNULL(UD.IdentityStatus,'') NOT IN ('D','U','L') 
    AND ISNULL(UD.EmployeeType,'') NOT IN ('O','V','') 
    AND ISNULL(UD.HltId,'') != '' 
    AND ISNULL(UD.EmpId,'') NOT IN (SELECT DISTINCT UserId FROM Search..CurrentUserFile) 
+1

你有上MCU.EmpID,UD.EmpId,UD.UserId,SU.UserID,SU.HomeFacility,NQ.FCNB,NQ.SubBrand和FSBD指標。 SubBrand。同樣在UD.IdentityStatus,UD.EmployeeType和UD.HltId上? – ankurtr 2013-05-13 05:53:20

+0

SELECT沒有插入需要多長時間? – 2013-05-13 09:23:53

回答

2

你試過避免了DISTINCT查詢內部查詢?

Search..CurrentUserFile的大小是多少?

嘗試這樣的事情 -

SELECT 
.... 
FROM Search..UserData UD 
    ... -- all your earlier joins as it is 
    LEFt JOIN Search..CurrentUserFile CU on (UD.EmpId=CU.UserId) 
WHERE 
    ... -- all your where clause 
    AND CU.UserId IS NULL;--only show results which are not in CurrentUserFile 
1

嘗試這一個 -

SELECT ... 
FROM (
    SELECT * 
    FROM Search.dbo.UserData UD 
    WHERE ISNULL(UD.UserId, '') != '' 
     AND ISNULL(UD.IdentityStatus, '') NOT IN ('D','U','L') 
     AND ISNULL(UD.EmployeeType, '') NOT IN ('O','V','') 
     AND ISNULL(UD.HltId, '') != '' 
     AND ISNULL(UD.EmpId, '') NOT IN (
         SELECT DISTINCT UserId 
         FROM Search.dbo.CurrentUserFile 
        ) 
) UD 
LEFT JOIN Search.dbo.ManagerForCorpUsers MCU ON MCU.EmpID = UD.EmpId 
LEFT JOIN Search.dbo.securityUsers SU ON UD.UserId = SU.UserID 
LEFT JOIN EIS.dbo.NewQueryFilter NQ ON SU.HomeFacility = NQ.FCNB 
    AND ISNULL(SU.HomeFacility, '') != '' 
LEFT JOIN Facility.dbo.fcSubBrandDesc FSBD ON NQ.SubBrand = FSBD.SubBrand 
    AND ISNULL(NQ.SubBrand, '') != '' 
+0

並且不要使用「」/''([COLUMN] AS'COLUMN NAME'),更優選的是使用方括號作爲列名 - [COLUMN] AS [COLUMN NAME] – Devart 2013-05-13 06:03:57

1

如果您使用的是恢復模型事務會記錄所有的,「你可以如果你有多個插件,發生使用大容量日誌記錄恢復,並儘量減少語句使用案例加盟而是和左而是不加入的條件