Teradata SQL tuning with Sum and other aggregate f

2019-08-06 21:08发布

I have a query like

    sel
tb1.col1,
tb4.col2,
(case WHEN t4.col4 in (<IN LIST with more than 1000 values!>) then T4.Col7 
Else "Flag" ) as "Dcol1",
Sum ( tb3.col1),
sum (tb3.col2 ),
sum (tb2.col4)
etc
from
tb1 left outer join tb2 <condition> LOJ tb3 <conditions>
where tb1 condition and tb2 condition and tb3 condition

group by ( case <condition> , colx.tb2,coly.tb1

Problem is TB3 and TB4 are HUGE fact table. The PI of the fact table is NOT included in the joins or Queries here.

What I have done so far

is create a volatile table ( same pi for IN LIST ) and tried to materialized. VT1 has SAME PI as TB4 and will include the IN LIST in the where clause. I LOJ this using the approach

select
       ....
       CASE WHEN Dtb1.c1 IS NOT NULL 
            THEN ft."CustomColumName" 
            ELSE  'ALL OTHER' 
       end as "CustomColumName" 
    from "Db"."FACTTablew5MillionRows" as ft
    left join VolatileTable Dtb1 
    on Dtb1.c1=ft.C1

HOW can I optimize these kinds of queries Assume tb3 and tb2 are huge fact tables. PI TB3 is C4, c6 and PI of tb2 is C6, C7 TB3 has a partitioning column Cp but is NOT used for any kind of where clause. It gets used in one of the joins they do NOT have the same PI but MAY have a column in common within their PI's Row counts are some 80 Millions rows for TB3 , 60 million . The original Query simply would not run without spooling out. Luckily at night it could with 80K Impact C. I COULD get the query to run in < 200 Impact ONLY after creating a VT2 with same PI as tb2 and then using it to join. I DO NOT want to create a bunch of VT to used by BO users who know squat about TD. WHAT can I do to make this Q better

0条回答
登录 后发表回答