日期:2014-05-16 浏览次数:20575 次
物化视图,它是用于预先计算并保存表连接或聚集等耗时较多的操作的结果,这样,在执行查询时,就可以避免进行这些耗时的操作,从而快速的得到结果。物化视图有很多方面和索引很相似:使用物化视图的目的是为了提高查询性能;物化视图对应用透明,增加和删除物化视图不会影响应用程序中SQL 语句的正确性和有效性;物化视图需要占用存储空间;当基表发生变化时,物化视图也应当刷新。 其中物化视图有三种:聚集物化视图、包含连接物化视图、嵌套物化视图。但三种物化视图的快速刷新的限制条件有很大区别,而其他方面则区别不大。 http://baike.baidu.com/view/3208435.htm
--创建 CREATE MATERIALIZED VIEW MV_VIEW AS {SELECT * FROM} --删除 DROP MATERIALIZED VIEW MV_VIEW AS {SELECT * FROM} --例如 create materialized view MV_SM_USERINFO refresh force on demand start with to_date('19-01-2012 11:00:00', 'dd-mm-yyyy hh24:mi:ss') next to_date(concat(to_char(SYSDATE+1,'dd-mm-yyyy'),'11:00:00'),'dd-mm-yyyy hh24:mi:ss') as select sm_user.pk_user,sm_user.user_name,sm_user.user_code,sm_user.password,sm_user.identity_no,sm_user.memo, sm_org.pk_org,sm_org.org_code,sm_org.org_name from sm_user,sm_org where sm_user.pk_org = sm_org.pk_org --这个物化视图使用了定时更新功能
--指定物化视图每天刷新一次 CREATE MATERIALIZED VIEW MV_VIEW REFRESH FORCE ON DEMAND START WITH SYSDATE NEXT SYSDATE+1 AS …… --指定物化视图每天晚上10:00点刷新一次 CREATE MATERIALIZED VIEW MV_VIEW REFRESH FORCE ON DEMAND START WITH SYSDATE NEXT TO_DATE(CONCAT(TO_CHAR(SYSDATE+1,'DD-MM-YYYY'),'22:00:00'),'DD-MM-YYYY HH24:MI:SS') AS
--语法 CREATE MATERIALIZED VIEW LOG ON TABLE_NAME WITH SEQUENCE,ROWID (NUM1,NUM2,NUM3,NUM4,NUM5,NUM6,NUM7 )INCLUDING NEW VALUES; --sm_user 建立日志 create materialized view log on sm_user with SEQUENCE,ROWID(pk_user,user_name,user_code,password,identity_no); --sm_org 建立日志 create materialized view log on sm_org with SEQUENCE,ROWID(pk_org,org_code,org_name); 查看日志 select * from mlog$_sm_org select * from mlog$_sm_user --查询日志 select * from dba_snapshot_logs --查询物化视图 select * from dba_snapshot_logs --查询oracle版本信息 select * from gv$version;
--存储过程 create or replace procedure MV_REFRESH_USERINFO is begin dbms_mview.refresh('MV_SM_USERINFO'); end MV_REFRESH_USERINFO; --线程调用从存储过程 private int count = 0; Connection orclcon; public void run(){ if(orclcon==null){ JDBCDao jdbc = new JDBCDao(); orclcon = jdbc.getMATERVIEWConnection(); } try { CallableStatement proc = orclcon.prepareCall("{call MV_REFRESH_USERINFO()}"); boolean result = proc.execute(); count++; System.out.println("call MV_REFRESH_USERINFO() " + result + ";已经运行: " + count); } catch (SQLException e) { e.printStackTrace(); } }