在连接条件下使用OR加入或使用多个JOINS更好

时间:2016-03-28 18:07:43

标签: sql performance join teradata

我试图根据票据是否表明发生了特定事件来标记票证。我遇到的问题是,表明发生这种情况的特定值可能在故障单上的25个字段中的任何一个中,并且我当前的逻辑需要相当长的时间才能完成(通常> 45分钟)如果它完全完成(我们的DBA偶尔会因为占用太多CPU或者PJI太高而杀死它)。目前我使用以下内容尝试标记门票:

active

所以我的问题是,每个事件CK切换到JOINing是否更好(这是一个关键值&是AOD_CKS上的索引)?或者我应该少一些冗长的&尝试拆分工作(即为字段1-6加入一次,为7-13加入另一个表等)&然后将这4张桌子加在一起?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我最终将JOIN分成3个部分,然后按如下方式合并数据:

----------------------------------------------------------------------------
--                                                  FLAG ADDICTION CLAIMS
----------------------------------------------------------------------------    
CREATE VOLATILE TABLE
--DROP TABLE 
I_FCT_1
AS ( 
SELECT
    I_FCT.Claim_Nbr
    , I_FCT.Plan_Dim_CK

    , CASE
        WHEN I_FCT.Diag1_CK = AD.Diag_CK THEN '1'
        WHEN I_FCT.Diag2_CK = AD.Diag_CK THEN '2'
        WHEN I_FCT.Diag3_CK = AD.Diag_CK THEN '3'
        WHEN I_FCT.Diag4_CK = AD.Diag_CK THEN '4'
        WHEN I_FCT.Diag5_CK = AD.Diag_CK THEN '5'
        WHEN I_FCT.Diag6_CK = AD.Diag_CK THEN '6'
        WHEN I_FCT.Diag7_CK = AD.Diag_CK THEN '7'
        WHEN I_FCT.Diag8_CK = AD.Diag_CK THEN '8'
        ELSE NULL
    END AS AOD_Diag

FROM
    I_FCT

    LEFT JOIN ADD_CKS AS AD
        ON (AD.Diag_CK = I_FCT.Diag1_CK
            OR AD.Diag_CK = I_FCT.Diag2_CK
            OR AD.Diag_CK = I_FCT.Diag3_CK
            OR AD.Diag_CK = I_FCT.Diag4_CK
            OR AD.Diag_CK = I_FCT.Diag5_CK
            OR AD.Diag_CK = I_FCT.Diag6_CK
            OR AD.Diag_CK = I_FCT.Diag7_CK
            OR AD.Diag_CK = I_FCT.Diag8_CK
        )
)
WITH DATA PRIMARY INDEX (Claim_Nbr) 
ON COMMIT PRESERVE ROWS;

----------------------------------------------------------------------------
CREATE VOLATILE TABLE
--DROP TABLE 
I_FCT_2
AS ( 
SELECT
    I_FCT.Claim_Nbr
    , I_FCT.Plan_Dim_CK

    , CASE
        WHEN I_FCT.Diag9_CK = AD.Diag_CK THEN '9'
        WHEN I_FCT.Diag10_CK = AD.Diag_CK THEN '10'
        WHEN I_FCT.Diag11_CK = AD.Diag_CK THEN '11'
        WHEN I_FCT.Diag12_CK = AD.Diag_CK THEN '12'
        WHEN I_FCT.Diag13_CK = AD.Diag_CK THEN '13'
        WHEN I_FCT.Diag14_CK = AD.Diag_CK THEN '14'
        WHEN I_FCT.Diag15_CK = AD.Diag_CK THEN '15'
        WHEN I_FCT.Diag16_CK = AD.Diag_CK THEN '16'
        ELSE NULL
    END AS AOD_Diag

FROM
    I_FCT

    LEFT JOIN ADD_CKS AS AD
        ON (AD.Diag_CK = I_FCT.Diag9_CK
            OR AD.Diag_CK = I_FCT.Diag10_CK
            OR AD.Diag_CK = I_FCT.Diag11_CK
            OR AD.Diag_CK = I_FCT.Diag12_CK
            OR AD.Diag_CK = I_FCT.Diag13_CK
            OR AD.Diag_CK = I_FCT.Diag14_CK
            OR AD.Diag_CK = I_FCT.Diag15_CK
            OR AD.Diag_CK = I_FCT.Diag16_CK
        )
)
WITH DATA PRIMARY INDEX (Claim_Nbr) 
ON COMMIT PRESERVE ROWS;

----------------------------------------------------------------------------
CREATE VOLATILE TABLE
--DROP TABLE 
I_FCT_3
AS ( 
SELECT
    I_FCT.Claim_Nbr
    , I_FCT.Plan_Dim_CK

    , CASE
        WHEN I_FCT.Diag17_CK = AD.Diag_CK THEN '17'
        WHEN I_FCT.Diag18_CK = AD.Diag_CK THEN '18'
        WHEN I_FCT.Diag19_CK = AD.Diag_CK THEN '19'
        WHEN I_FCT.Diag20_CK = AD.Diag_CK THEN '20'
        WHEN I_FCT.Diag21_CK = AD.Diag_CK THEN '21'
        WHEN I_FCT.Diag22_CK = AD.Diag_CK THEN '22'
        WHEN I_FCT.Diag23_CK = AD.Diag_CK THEN '23'
        WHEN I_FCT.Diag24_CK = AD.Diag_CK THEN '24'
        WHEN I_FCT.Diag25_CK = AD.Diag_CK THEN '25'
        ELSE NULL
    END AS AOD_Diag

FROM
    I_FCT

    LEFT JOIN ADD_CKS AS AD
        ON (AD.Diag_CK = I_FCT.Diag17_CK
            OR AD.Diag_CK = I_FCT.Diag18_CK
            OR AD.Diag_CK = I_FCT.Diag19_CK
            OR AD.Diag_CK = I_FCT.Diag20_CK
            OR AD.Diag_CK = I_FCT.Diag21_CK
            OR AD.Diag_CK = I_FCT.Diag22_CK
            OR AD.Diag_CK = I_FCT.Diag23_CK
            OR AD.Diag_CK = I_FCT.Diag24_CK
            OR AD.Diag_CK = I_FCT.Diag25_CK
        )
)
WITH DATA PRIMARY INDEX (Claim_Nbr) 
ON COMMIT PRESERVE ROWS;

---------------------------------------------------------------------------
CREATE VOLATILE TABLE
--DROP TABLE 
FCT
AS ( 
SELECT
    I_FCT.Claim_Nbr
    , I_FCT.Plan_Dim_CK
    , I_FCT.ADMISSION_DATE

    , I_FCT.DISCHARGE_DATE

    , I_FCT.ADMISSION_DATE_DIM_CK
    , I_FCT.Proc3_CK
    , I_FCT.Proc4_CK
    , I_FCT.Proc5_CK
    , I_FCT.Proc6_CK
    , I_FCT.Proc7_CK
    , I_FCT.Bill_Type
    , CASE
        WHEN C1.AOD_Diag IS NOT NULL THEN C1.AOD_Diag
        WHEN C2.AOD_Diag IS NOT NULL THEN C2.AOD_Diag
        WHEN C3.AOD_Diag IS NOT NULL THEN C3.AOD_Diag
        ELSE NULL
    END AS AOD_Diag

FROM
    I_FCT

    JOIN I_FCT_1 AS C1
        ON C1.Claim_Nbr = I_FCT.Claim_Nbr
            AND C1.Plan_Dim_CK = I_FCT.Plan_Dim_CK

    JOIN I_FCT_2 AS C2
        ON I_FCT.Claim_Nbr = C2.Claim_Nbr
            AND I_FCT.Plan_Dim_CK = C2.Plan_Dim_CK

    JOIN I_FCT_3 AS C3
        ON I_FCT.Claim_Nbr = C3.Claim_Nbr
            AND I_FCT.Plan_Dim_CK = C3.Plan_Dim_CK
)
WITH DATA PRIMARY INDEX (Claim_Nbr)
ON COMMIT PRESERVE ROWS;