Teradata查询需要优化

时间:2015-09-23 16:18:39

标签: optimization teradata

还有其他方法可以编写以下声明:

(select coalesce(max(ch.chistdate),to_date('01011900','MMDDYYYY'))
      from mydb.chist ch 
      where ch.cactionid in (1,2) 
        and ch.chistdate < inv.jdate 
       and ch.cid = cbo.cid)     = (select max(ch.chistdate) 
                                                from mydb.chist ch 
                                                where ch.cactionid in (3,4) 
                                                  and ch.chistdate < inv.jdate 
                                                  and ch.cid = cbo.cid)

此语句是非常大的查询的一部分,并包含在where部分中这是唯一没有给出假脱机空间错误的部分。我检查了用于此的表格,他们迅速给出结果。

主要查询在这里:

select *
from   (select inv.domainnm
  , count(distinct cbo.cid) as numbo
  , trunc(inv.asdate) as asdate
from mydb.inv inv
                  join mydb.ComittedBO cbo on (inv.domainnm = cbo.domainnm 
                                      and inv.jdate >= cbo.sdate
                                      and coalesce(cbo.edate, to_date('01-JAN-3000','DD-MON-RRRR')) >= inv.jdate
                                      and cbo.cid not in (select cpp.cid 
                                                                 from mydb.cpprices cpp
                                                                 where NOT EXISTS (select 1 from mydb.cpitems cpt where cpp.cid = cpt.cid)
                                                                   and trunc(cpp.sdate) <= current_date
                                                                   and ((to_number(to_char(current_date, 'dd')) = 1
                                                                       and coalesce(trunc(cpp.edate),trunc(current_date+1)) >= add_months(trunc(current_date,'mm'),-1))
                                                                       or coalesce(trunc(cpp.edate),trunc(current_date+1)) >= trunc(current_date,'month'))))

                                                                       where ((NOT EXISTS (select 1
            from mydb.customers cu
            where cu.statusid = 3
              and cu.cid = cbo.cid)
   and NOT EXISTS (select 1 
          from mydb.chist ch 
          where ch.cactionid in (3,4) 
            and ch.chistdate < inv.jdate 
            and ch.cid = cbo.cid))



  or
  ((select coalesce(max(ch.chistdate),to_date('01011900','MMDDYYYY'))
      from mydb.chist ch 
      where ch.cactionid in (1,2) 
        and ch.chistdate < inv.jdate 
       and ch.cid = cbo.cid) in (select max(ch.chistdate) 
                                                from mydb.chist ch 
                                                where ch.cactionid in (3,4) 
                                                  and ch.chistdate < inv.jdate 
                                                  and ch.cid = cbo.cid  )
                                                  )   



                                                  )
group by inv.domainnm,trunc(inv.asdate)) inventory
  left join mydb.ahist ah on (inventory.domainnm = ah.domainnm 
                                  and ah.aaid = 7 

                                    and ah.astatusid in (7,9,14,17,19)
                                  and ah.asdate + interval'1' day >= inventory.asdate
                                  and inventory.asdate + interval'1' day >= ah.asdate)
  left join mydb.auction au on (inventory.domainnm = au.domainnm
                               and au.asdate + interval'1' day >= inventory.asdate
                               and inventory.asdate + interval'1' day >= au.asdate)
  left join sdb.harv hv on (inventory.domainnm = hv.domainnm
                                  and hv.sndate + interval'1' day >= inventory.asdate
                                  and inventory.asdate + interval'1' day >= hv.sndate)
  left join mydb.customers "cs" on ah.cid = "cs".cid
  left join mydb.aresult ar on ah.aid = ar.aid
  left join mydb.cdphist cdph on ar.cdpid = cdph.cdpid
  left join mydb.reg rgr on cdph.rid = rgr.rid
where ((to_number(to_char(current_date, 'dd')) = 1
      and inventory.asdate >= add_months(trunc(current_date,'mm'),-1))
      or inventory.asdate >= trunc(current_date,'month'))
  and inventory.asdate < current_date

感谢。

2 个答案:

答案 0 :(得分:0)

只关注您的Inventory派生表,我将从WHERE子句中提出问题的两个SELECT语句移动到带有LEFT OUTER JOIN的FROM子句。

我还将它们合并为一个包含三个字段的语句:cid用于加入cbo表,然后是两个CASE语句用于每组{{1} (1和2,3和4)。然后,在WHERE子句中,我刚刚测试了每个cactionid的{​​{1}}相等的位置。

max(chistdate)

我认为通过更多地处理这个查询可以获得更多的效率,但希望这会得到相同的结果(我相信它会,但没有看到你的数据库架构和一些示例数据,它&#39 ;很难重写像这样的复杂的东西),希望它能在不堵塞你有限的假脱机空间的情况下运行。

答案 1 :(得分:0)

那么挥发性表可能会解决您的问题。在主查询之前创建一个易失性表,如下所示: -

create volatile table tab1
(
chistdateM  date format 'MMDDYYYY'
)primary index(chistdate)
on commit preserve rows;

      insert into tab1 `enter code here`
      select coalesce(max(ch.chistdate),to_date('01011900','MMDDYYYY')) as chistdate
      from mydb.chist ch  , mydb.ComittedBO cbo
      where ch.cactionid in (1,2) 
      and ch.chistdate < inv.jdate 
      and ch.cid = cbo.cid;

create volatile table tab2
(
chistdateS  date format 'MMDDYYYY'
)primary index(chistdate)
on commit preserve rows;
      insert into tab2
      select max(ch.chistdate) from mydb.chist ch 
      where ch.cactionid in (3,4) 
      and ch.chistdate < inv.jdate 
      and ch.cid = cbo.cid  ; 

并在你的逆变派生表中使用它们: -

select * from   (select inv.domainnm, count(distinct cbo.cid) as numbo , trunc(inv.asdate) as asdate
from mydb.inv inv  join mydb.ComittedBO cbo on (inv.domainnm = cbo.domainnm    and inv.jdate >= cbo.sdate
                                      and coalesce(cbo.edate, to_date('01-JAN-3000','DD-MON-RRRR')) >= inv.jdate
                                      and cbo.cid not in (select cpp.cid from mydb.cpprices cpp
                                                                 where NOT EXISTS (select 1 from mydb.cpitems cpt where cpp.cid = cpt.cid)
                                                                   and trunc(cpp.sdate) <= current_date
                                                                   and ((to_number(to_char(current_date, 'dd')) = 1
                                                                       and coalesce(trunc(cpp.edate),trunc(current_date+1)) >= add_months(trunc(current_date,'mm'),-1))
                                                                       or coalesce(trunc(cpp.edate),trunc(current_date+1)) >= trunc(current_date,'month'))))
                                                                       where ((NOT EXISTS (select 1
            from mydb.customers cu
            where cu.statusid = 3
              and cu.cid = cbo.cid)
   and NOT EXISTS (select 1 
          from mydb.chist ch 
          where ch.cactionid in (3,4) 
            and ch.chistdate < inv.jdate 
            and ch.cid = cbo.cid))
  or
  ( Select * from tab1 where  chistdateM in (select chistdateS from tab2)    )   
                                                  )
group by inv.domainnm,trunc(inv.asdate)) inventory
  left join mydb.ahist ah on (inventory.domainnm = ah.domainnm 
                                  and ah.aaid = 7 

                                    and ah.astatusid in (7,9,14,17,19)
                                  and ah.asdate + interval'1' day >= inventory.asdate
                                  and inventory.asdate + interval'1' day >= ah.asdate)
  left join mydb.auction au on (inventory.domainnm = au.domainnm
                               and au.asdate + interval'1' day >= inventory.asdate
                               and inventory.asdate + interval'1' day >= au.asdate)
  left join sdb.harv hv on (inventory.domainnm = hv.domainnm
                                  and hv.sndate + interval'1' day >= inventory.asdate
                                  and inventory.asdate + interval'1' day >= hv.sndate)
  left join mydb.customers "cs" on ah.cid = "cs".cid
  left join mydb.aresult ar on ah.aid = ar.aid
  left join mydb.cdphist cdph on ar.cdpid = cdph.cdpid
  left join mydb.reg rgr on cdph.rid = rgr.rid
where ((to_number(to_char(current_date, 'dd')) = 1
      and inventory.asdate >= add_months(trunc(current_date,'mm'),-1))
      or inventory.asdate >= trunc(current_date,'month'))
  and inventory.asdate < current_date

请根据您的要求验证您的数据。