0

我的理解是,大多数 OR 条件都可以在 UNION ALL 的性能优势处被替换。但是对于这 2 个查询,Count ('1') 是不一样的。为什么会这样-我在这里遗漏了什么。应该不一样吧。有人能解释一下差距吗

SQL #1

sel 
D1.COL_1_CD, D1.COL_1_DESC,
        D2.COL_2_CD, D2.COL_2_DESC, D3.COL_3_CD, D3.COL_3_DESC, D4.COL_4_CD,
        D4.COL_4_DESC, D5.COL_5_CD, D5.COL_5_DESC,
        d1.COL_1_CD_SYS_ID,
        d2.COL_2_CD_SYS_ID,
        d3.COL_3_CD_SYS_ID,
        d4.COL_4_CD_SYS_ID,
        d5.COL_5_CD_SYS_ID


from
 D1,
 D2,  D3,  D4,
         D5

        where
        D1.COL_1_CD1=D2.COL_2_CD1  
    and
        D2.COL_2_CD1=D3.COL_3_CD1 
    and 
        D4.COL_4_CD1=D5.COL_5_CD1 
        and 


 (D1.COL_1_CD in ('707') 
    or   D2.COL_2_CD in ('707') 
    or  D3.COL_3_CD in ('707') 
    or  D4.COL_4_CD in ('707') 
    or   D5.COL_5_CD in ('707') ) 

    group by 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

SQL #2

sel 
D1.COL_1_CD, D1.COL_1_DESC,
        D2.COL_2_CD, D2.COL_2_DESC, D3.COL_3_CD, D3.COL_3_DESC, D4.COL_4_CD,
        D4.COL_4_DESC, D5.COL_5_CD, D5.COL_5_DESC,
        d1.COL_1_CD_SYS_ID,
        d2.COL_2_CD_SYS_ID,
        d3.COL_3_CD_SYS_ID,
        d4.COL_4_CD_SYS_ID,
        d5.COL_5_CD_SYS_ID


from
 D1,
 D2,  D3,  D4,
         D5

        where
        D1.COL_1_CD1=D2.COL_2_CD1  
    and
        D2.COL_2_CD1=D3.COL_3_CD1 
    and 
        D4.COL_4_CD1=D5.COL_5_CD1 
        and 


 D1.COL_1_CD in ('707') 

UNION ALL 

sel 
D1.COL_1_CD, D1.COL_1_DESC,
        D2.COL_2_CD, D2.COL_2_DESC, D3.COL_3_CD, D3.COL_3_DESC, D4.COL_4_CD,
        D4.COL_4_DESC, D5.COL_5_CD, D5.COL_5_DESC,
        d1.COL_1_CD_SYS_ID,
        d2.COL_2_CD_SYS_ID,
        d3.COL_3_CD_SYS_ID,
        d4.COL_4_CD_SYS_ID,
        d5.COL_5_CD_SYS_ID


from
 D1,
 D2,  D3,  D4,
         D5

        where
        D1.COL_1_CD1=D2.COL_2_CD1  
    and
        D2.COL_2_CD1=D3.COL_3_CD1 
    and 
        D4.COL_4_CD1=D5.COL_5_CD1 
        and 


   D2.COL_2_CD in ('707')  


UNION ALL 
.....<same query>
D3.COL_3_CD in ('707')

UNION ALL 

.....<same query>
D4.COL_4_CD in ('707')

UNION ALL 
.....<same query>
D5.COL_5_CD in ('707')

行数不一样。什么样的OR逻辑可以转换为等价的UNION ALL。

4

0 回答 0