关闭

SQL 优化之 oracle物化视图

标签: sqloracle优化joinuserfile
2188人阅读 评论(0) 收藏 举报
分类:

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

    供自己学习和借鉴……

   可惜的是 也看的很少……

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

   目前最大的工作就是优化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秒……

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

 

所以很耗费资源……

恩……

 

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:171381次
    • 积分:2689
    • 等级:
    • 排名:第13461名
    • 原创:96篇
    • 转载:7篇
    • 译文:0篇
    • 评论:23条
    最新评论
    技术收藏和参考