左联接视图在SQL Server中花费太多时间

时间:2018-12-02 11:05:44

标签: sql-server-2012 query-performance

此查询需要20秒才能显示53,000条记录。该查询具有五个左连接和视图。但是,当我注释掉select语句中的MAN.F_PHR AS MANU列时,需要三秒钟才能显示53,000条记录。

如何优化此查询?视图V_PROD_ALIAS_MANU MAN包含两个左联接。 MAN.F_PHRE AS MANU列来自视图。有什么方法可以优化此查询?

SELECT DISTINCT 
    TP.PRODUCT AS ID,
    TP.NAME AS [NAME],
    TP.LANGUAGE AS LANGCODE,
    CONVERT(VARCHAR,TP.F_DATE_REVISED,120) AS RDATE,
    CASE
       WHEN NOT(REPLACE(LTRIM(RTRIM(REPLACE(TP.F_CAS_NUM,'¿',' '))),' ','; ') IS NULL OR REPLACE(LTRIM(RTRIM(REPLACE(TP.F_CAS_NUM,'¿',' '))),' ','; ') = '') 
          THEN REPLACE(LTRIM(RTRIM(REPLACE(TP.F_CAS_NUM,'¿',' '))),' ','; ') 
          ELSE REPLACE(LTRIM(RTRIM(REPLACE(CASN.F_DATA,'¿',' '))),' ','; ')
    END AS CASNUM ,
    TP.F_CUSTOM1 AS cus1,
    TP.F_CUSTOM2 AS cus2,
    (SELECT TC.F_COUNTRY_NAME FROM COUNTRIES TC 
     WHERE TC.F_COUNTRY_CODE = TP.F_CUSTOM5) AS cus5,
    MAN.F_PHR AS MANU,
    CASE
       WHEN NOT(TP.F_CUSTOM3 IS NULL OR TP.F_CUSTOM3 = '') 
          THEN TP.F_CUSTOM3 
          ELSE SYN.F_DATA
    END AS SYN, 
    IC.F_DATA AS ICO,
    'SDS - ' + (SELECT TL.F_LAN_NAME FROM T_LANGUAGE TL 
                WHERE TL.F_LANGUAGE = TP.F_LANGUAGE) + ' - PDF' AS DOC,
    '' AS COVER,
    CAST(TP.F_GUID AS VARCHAR(36)) + '_PDF' AS [GUID],
    'PDF' AS SDS
FROM 
    PDF TP
LEFT JOIN 
    V_PROD_ALIAS_SYN SYN ON TP.F_PRODUCT = SYN.F_PRODUCT
LEFT JOIN 
    V_PROD_ALIAS_SITE SIT ON TP.F_PRODUCT = SIT.F_PRODUCT
LEFT JOIN 
    V_PROD_ALIAS_ICO IC ON TP.F_PRODUCT = IC.F_PRODUCT
LEFT JOIN 
    V_PROD_ALIAS_MANU MAN ON TP.F_PRODUCT = MAN.F_PRODUCT
LEFT JOIN 
    V_PROD_ALIAS_CASN CASN ON TP.F_PRODUCT = CASN.F_PRODUCT

将列MAN.F_PHRE AS MANU注释掉后进行查询需要三秒钟才能显示53,000条记录。

SELECT DISTINCT 
    TP.PRODUCT AS ID,
    TP.NAME AS [NAME],
    TP.LANGUAGE AS LANGCODE,
    CONVERT(VARCHAR, TP.F_DATE_REVISED, 120) AS RDATE,
    CASE
       WHEN NOT(REPLACE(LTRIM(RTRIM(REPLACE(TP.F_CAS_NUM,'¿',' '))),' ','; ') IS NULL OR REPLACE(LTRIM(RTRIM(REPLACE(TP.F_CAS_NUM,'¿',' '))),' ','; ') = '') 
          THEN REPLACE(LTRIM(RTRIM(REPLACE(TP.F_CAS_NUM,'¿',' '))),' ','; ') 
          ELSE REPLACE(LTRIM(RTRIM(REPLACE(CASN.F_DATA,'¿',' '))),' ','; ')
    END AS CASNUM ,
    TP.F_CUSTOM1 AS cus1,
    TP.F_CUSTOM2 AS cus2,
    (SELECT TC.F_COUNTRY_NAME FROM COUNTRIES TC 
     WHERE TC.F_COUNTRY_CODE = TP.F_CUSTOM5) AS cus5,
    -- MAN.F_PHR AS MANU,
    CASE
       WHEN NOT(TP.F_CUSTOM3 IS NULL OR TP.F_CUSTOM3 = '') 
          THEN TP.F_CUSTOM3 
          ELSE SYN.F_DATA
    END AS SYN, 
    IC.F_DATA AS ICO,
    'SDS - ' + (SELECT TL.F_LAN_NAME FROM T_LANGUAGE TL 
                WHERE TL.F_LANGUAGE = TP.F_LANGUAGE) + ' - PDF' AS DOC,
    '' AS COVER,
    CAST(TP.F_GUID AS VARCHAR(36)) + '_PDF' AS [GUID],
    'PDF' AS SDS
FROM 
    PDF TP
LEFT JOIN 
    V_PROD_ALIAS_SYN SYN ON TP.F_PRODUCT = SYN.F_PRODUCT
LEFT JOIN 
    V_PROD_ALIAS_SITE SIT ON TP.F_PRODUCT = SIT.F_PRODUCT
LEFT JOIN 
    V_PROD_ALIAS_ICO IC ON TP.F_PRODUCT = IC.F_PRODUCT
LEFT JOIN 
    V_PROD_ALIAS_MANU MAN ON TP.F_PRODUCT = MAN.F_PRODUCT
LEFT JOIN 
    V_PROD_ALIAS_CASN CASN ON TP.F_PRODUCT = CASN.F_PRODUCT

0 个答案:

没有答案
相关问题
最新问题