SQL 优化之 oracle物化视图

     在这里,其实大多都是我摘抄的参考资料……

    供自己学习和借鉴……

   可惜的是 也看的很少……

   塔里木物流系统实施快三个月了,灾难的生活也半年了……

   目前最大的工作就是优化sql……

 

  这次,北京派来个真正的dba……

 

 学会了物化视图!感觉很方便……

 

oracle物化视图
                                      


物化视图对于前台数据库使用者来说如同一个实际的表,具有和表相通的一般select操作,而其实际上是一个视图,一个定期刷新数据的视图(具体刷新时间在定义物化视图的时候已有定义),使用物化视图可以实现视图的所有功能,而物化视图确不是在使用时才读取,大大提高了读取速度,特别适用抽取大数据量表某些信息以及数据链连接表使用.具体语法如下:

CREATE MATERIALIZED VIEW an_user_base_file_no_charge
             REFRESH COMPLETE START WITH SYSDATE 
            NEXT  TRUNC(SYSDATE+29)+5.5/24
   as
select distinct user_no
  from cw_arrearage t
 where (t.mon = dbms_tianjin.getLastMonth or
       t.mon = add_months(dbms_tianjin.getLastMonth, -1))

drop materialized view an_user_base_file_no_charge;

 

比如一个很复杂的sql

select tt.CGFS       CGFS,
       tt.XQJHID     XQJHID,
       tt.XQJHBH     XQJHBH,
       tt.YHZBH      YHZBH,
       tt.TMBH       TMBH,
       tt.JHLX       JHLX,
       tt.JHSXBH     JHSXBH,
       tt.SXRQ       SXRQ,
       tt.JYHSFSBH   JYHSFSBH,
       tt.XMBH       XMBH,
       tt.XMMC       XMMC,
       tt.WZSXBH     WZSXBH,
       tt.GHQDBH     GHQDBH,
       tt.JHLBBH     JHLBBH,
       tt.ZZSXBH     ZZSXBH,
       tt.ZCZJLYBH   ZCZJLYBH,
       tt.SFCG       SFCG,
       tt.SFDL       SFDL,
       tt.CGDWBH     CGDWBH,
       tt.CGDWMC     CGDWMC,
       tt.SSDWBH     SSDWBH,
       tt.SSDWMC     SSDWMC,
       tt.DLRGWBH    DLRGWBH,
       tt.DLRGWMC    DLRGWMC,
       tt.DLRBH      DLRBH,
       tt.DLRMC      DLRMC,
       tt.DLRQ       DLRQ,
       tt.ZDGWBH     ZDGWBH,
       tt.ZDGWMC     ZDGWMC,
       tt.ZDRBH      ZDRBH,
       tt.ZDRMC      ZDRMC,
       tt.CGFAJHZBH  CGFAJHZBH,
       tt.CGFATBRQ   CGFATBRQ,
       tt.ZDRQ       ZDRQ,
       tt.BZ         BZ,
       tt.GSJE       GSJE,
       tt.JHXS       JHXS,
       tt.cgfagysbh  cgfagysbh,
       tt.cgfagysmc  cgfagysmc,
       tt.cgfajhbh   cgfajhbh,
       tt.cgfatbdwbh cgfatbdwbh,
       tt.cgfatbdwmc cgfatbdwmc,
       tt.mxzt       mxzt,
       tt.xqjhmc     xqjhmc,
       tt.SXKZZT     SXKZZT,
       tt.bjdbz      bjdbz
  from (select cc.cgfs, cc.mxzt, ab.*
          from (select distinct c.xqjhid, a.cgfs, a.mxzt
                  from jh_b_xqjhmx a, jh_b_jhzdmxdy c
                 where 1 = 1
                   and a.xqjhmxbh = c.ysjhmxbh
                   and ((a.mxzt >= 28 and a.mxzt < 200) or a.mxzt in (12, 13))
                   and (a.flgw2 like '01010109%' escape '/')) cc
         inner join (select e.XQJHID     XQJHID,
                           e.sxkzzt     sxkzzt,
                           e.XQJHBH     XQJHBH,
                           e.xqjhmc     xqjhmc,
                           e.YHZBH      YHZBH,
                           e.TMBH       TMBH,
                           e.JHLX       JHLX,
                           e.JHSXBH     JHSXBH,
                           e.SXRQ       SXRQ,
                           e.JYHSFSBH   JYHSFSBH,
                           e.XMBH       XMBH,
                           e.XMMC       XMMC,
                           e.WZSXBH     WZSXBH,
                           e.GHQDBH     GHQDBH,
                           e.JHLBBH     JHLBBH,
                           e.ZZSXBH     ZZSXBH,
                           e.ZCZJLYBH   ZCZJLYBH,
                           e.SFCG       SFCG,
                           e.SFDL       SFDL,
                           e.CGDWBH     CGDWBH,
                           e.CGDWMC     CGDWMC,
                           e.SSDWBH     SSDWBH,
                           e.SSDWMC     SSDWMC,
                           e.DLRGWBH    DLRGWBH,
                           e.DLRGWMC    DLRGWMC,
                           e.DLRBH      DLRBH,
                           e.DLRMC      DLRMC,
                           e.DLRQ       DLRQ,
                           e.CGFAJHZBH  CGFAJHZBH,
                           e.CGFATBRQ   CGFATBRQ,
                           e.ZDGWBH     ZDGWBH,
                           e.ZDGWMC     ZDGWMC,
                           e.ZDRBH      ZDRBH,
                           e.ZDRMC      ZDRMC,
                           e.ZDRQ       ZDRQ,
                           e.BZ         BZ,
                           e.GSJE       GSJE,
                           e.JHXS       JHXS,
                           e.cgfagysbh  cgfagysbh,
                           e.cgfagysmc  cgfagysmc,
                           e.cgfajhbh   cgfajhbh,
                           e.cgfatbdwbh cgfatbdwbh,
                           e.cgfatbdwmc cgfatbdwmc,
                           ttt.bjdbz    bjdbz
                      from jh_b_xqjh e,
                           (select distinct n.cgfabh, m.bjdbz
                              from cg_b_xbjd m, cg_b_xbjdmx n
                             where m.xjdbh = n.xjdbh) ttt
                     where jhlx != 1
                       and jhlx != 2
                       and e.xqjhbh = ttt.cgfabh(+)) ab on cc.xqjhid =
                                                           ab.xqjhid) tt
 order by tt.mxzt, tt.CGFATBRQ desc
调整成
select tt.CGFS       CGFS,
       tt.XQJHID     XQJHID,
       tt.XQJHBH     XQJHBH,
       tt.YHZBH      YHZBH,
       tt.TMBH       TMBH,
       tt.JHLX       JHLX,
       tt.JHSXBH     JHSXBH,
       tt.SXRQ       SXRQ,
       tt.JYHSFSBH   JYHSFSBH,
       tt.XMBH       XMBH,
       tt.XMMC       XMMC,
       tt.WZSXBH     WZSXBH,
       tt.GHQDBH     GHQDBH,
       tt.JHLBBH     JHLBBH,
       tt.ZZSXBH     ZZSXBH,
       tt.ZCZJLYBH   ZCZJLYBH,
       tt.SFCG       SFCG,
       tt.SFDL       SFDL,
       tt.CGDWBH     CGDWBH,
       tt.CGDWMC     CGDWMC,
       tt.SSDWBH     SSDWBH,
       tt.SSDWMC     SSDWMC,
       tt.DLRGWBH    DLRGWBH,
       tt.DLRGWMC    DLRGWMC,
       tt.DLRBH      DLRBH,
       tt.DLRMC      DLRMC,
       tt.DLRQ       DLRQ,
       tt.ZDGWBH     ZDGWBH,
       tt.ZDGWMC     ZDGWMC,
       tt.ZDRBH      ZDRBH,
       tt.ZDRMC      ZDRMC,
       tt.CGFAJHZBH  CGFAJHZBH,
       tt.CGFATBRQ   CGFATBRQ,
       tt.ZDRQ       ZDRQ,
       tt.BZ         BZ,
       tt.GSJE       GSJE,
       tt.JHXS       JHXS,
       tt.cgfagysbh  cgfagysbh,
       tt.cgfagysmc  cgfagysmc,
       tt.cgfajhbh   cgfajhbh,
       tt.cgfatbdwbh cgfatbdwbh,
       tt.cgfatbdwmc cgfatbdwmc,
       tt.mxzt       mxzt,
       tt.xqjhmc     xqjhmc,
       tt.SXKZZT     SXKZZT,
       tt.bjdbz      bjdbz
  from mv_test tt
 order by tt.mxzt, tt.CGFATBRQ desc
就可以了。]

mv_test tt

 

create materialized view MV_TEST
refresh force on demand
as
select cc.cgfs, cc.mxzt, ab.*
          from (select distinct c.xqjhid, a.cgfs, a.mxzt
                  from wz_wlpt.jh_b_xqjhmx a, wz_wlpt.jh_b_jhzdmxdy c
                 where 1 = 1
                   and a.xqjhmxbh = c.ysjhmxbh
                   and ((a.mxzt >= 28 and a.mxzt < 200) or a.mxzt in (12, 13))
                   and (a.flgw2 like '01010109%' escape '/')) cc
         inner join (select e.XQJHID     XQJHID,
                           e.sxkzzt     sxkzzt,
                           e.XQJHBH     XQJHBH,
                           e.xqjhmc     xqjhmc,
                           e.YHZBH      YHZBH,
                           e.TMBH       TMBH,
                           e.JHLX       JHLX,
                           e.JHSXBH     JHSXBH,
                           e.SXRQ       SXRQ,
                           e.JYHSFSBH   JYHSFSBH,
                           e.XMBH       XMBH,
                           e.XMMC       XMMC,
                           e.WZSXBH     WZSXBH,
                           e.GHQDBH     GHQDBH,
                           e.JHLBBH     JHLBBH,
                           e.ZZSXBH     ZZSXBH,
                           e.ZCZJLYBH   ZCZJLYBH,
                           e.SFCG       SFCG,
                           e.SFDL       SFDL,
                           e.CGDWBH     CGDWBH,
                           e.CGDWMC     CGDWMC,
                           e.SSDWBH     SSDWBH,
                           e.SSDWMC     SSDWMC,
                           e.DLRGWBH    DLRGWBH,
                           e.DLRGWMC    DLRGWMC,
                           e.DLRBH      DLRBH,
                           e.DLRMC      DLRMC,
                           e.DLRQ       DLRQ,
                           e.CGFAJHZBH  CGFAJHZBH,
                           e.CGFATBRQ   CGFATBRQ,
                           e.ZDGWBH     ZDGWBH,
                           e.ZDGWMC     ZDGWMC,
                           e.ZDRBH      ZDRBH,
                           e.ZDRMC      ZDRMC,
                           e.ZDRQ       ZDRQ,
                           e.BZ         BZ,
                           e.GSJE       GSJE,
                           e.JHXS       JHXS,
                           e.cgfagysbh  cgfagysbh,
                           e.cgfagysmc  cgfagysmc,
                           e.cgfajhbh   cgfajhbh,
                           e.cgfatbdwbh cgfatbdwbh,
                           e.cgfatbdwmc cgfatbdwmc,
                           ttt.bjdbz    bjdbz
                      from wz_wlpt.jh_b_xqjh e,
                           (select distinct n.cgfabh, m.bjdbz
                              from wz_wlpt.cg_b_xbjd m, wz_wlpt.cg_b_xbjdmx n
                             where m.xjdbh = n.xjdbh) ttt
                     where jhlx != 1
                       and jhlx != 2
                       and e.xqjhbh = ttt.cgfabh(+)) ab on cc.xqjhid =ab.xqjhid

 

 

这样原来的sql执行 要用1.433秒多,而使用物化视图执行才0.2秒……

但也不能多用,因为他需要实时来刷新视图才能保持和基表的一致……

 

所以很耗费资源……

恩……

 

不管怎么样,又学了点东西……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值