Teradata SQL:OR条件vs Union all

时间:2015-10-01 20:34:48

标签: sql join teradata sql-tuning

我的理解是,大多数OR条件可以替换为UNION ALL的性能优势。但对于这两个查询,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。

0 个答案:

没有答案