物化视图对于前台数据库使用者来说如同一个实际的表,具有和表相通的一般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秒……
但也不能多用,因为他需要实时来刷新视图才能保持和基表的一致……
所以很耗费资源……
恩……
不管怎么样,又学了点东西……
from :
阅读(2156) | 评论(0) | 转发(0) |