+ -
当前位置:首页 → 问答吧 → 利用视图可以提高sql效率么?

利用视图可以提高sql效率么?

时间:2011-12-19

来源:互联网

有一条sql,需要取三个表某个字段的合集,故想通过建视图提高查询的效率。不知道这方法可行不?

附上sql:


select /*+ parallel(his,8) */ his.idva, b.opnid  
from 
(select mobile from CH_BBC_ALLSALES where calcmonth='201111' union all  
select mobile from CH_BBC_CONTDATA where calcmonth='201111' union all 
select mobile from CH_BBC_WADTTASK where calcmonth='201111') t, CH_BBC_JFOPNMAP b, DSMPUSERDATA_HIS his  
 where  
 his.idva=t.mobile 
 and his.spid=b.entid 
 and his.SPBIZCODE=b.busiid
 and his.opr='07' 
 and to_date(his.efftt,'yyyymmddhh24miss') between add_months(to_date('20111101','yyyymmdd'),-2) and to_date('20111130235959','yyyymmddhh24miss') 
 union all 
 select a.servnumber, b.opnid from 
 (select /*+ parallel(b,8) */ b.subsid, b.canceloid, c.opnid from CM_SUBS_SPSERVICE b, CH_BBC_JFOPNMAP c  
  where b.spid=c.entid and b.spbizid=c.busiid) b,
  
  (select /*+ parallel(rec,8) */ rec.servnumber, rec.entityid, rec.oid from  
  (select mobile from CH_BBC_ALLSALES where calcmonth='201111' union all 
  select mobile from CH_BBC_CONTDATA where calcmonth='201111' union all 
  select mobile from CH_BBC_WADTTASK where calcmonth='201111') t, CS_REC_RECEPTION rec  
  where rec.servnumber=t.mobile 
  and rec.recdefid='Cancel' 
  and rec.recdate between add_months(to_date('20111101','yyyymmdd'),-2) and to_date('20111130235959','yyyymmddhh24miss')) a  
  where a.entityid=b.subsid and a.oid=b.canceloid;

作者: haiying2099   发布时间: 2011-12-19

视图不可能

作者: forgetsam   发布时间: 2011-12-19

那有什么方法可以优化下?

作者: haiying2099   发布时间: 2011-12-19

我觉得你这个地方不是视图的问题,而是语句本身就有点复杂,你可以先把结果保存到中间表,然后你再拿中间表的数据,跑到中间表的过程以跑批的形式执行.

作者: BearFishShow   发布时间: 2011-12-19

物化视图可以

作者: viszl   发布时间: 2011-12-19

sql写的太复杂了吧?

作者: zhangna08   发布时间: 2011-12-19

引用 3 楼 bearfishshow 的回复:
我觉得你这个地方不是视图的问题,而是语句本身就有点复杂,你可以先把结果保存到中间表,然后你再拿中间表的数据,跑到中间表的过程以跑批的形式执行.


增加中间表会增加数据的空间,这个不现实呀,就算是中间表,数据也可能上千万数据的。

作者: haiying2099   发布时间: 2011-12-19

引用 4 楼 viszl 的回复:
物化视图可以


这个是否会增加数据的占用空间?

作者: haiying2099   发布时间: 2011-12-19