创建每5分钟刷新一次的物化视图

时间:2012-04-08 08:51:25

标签: oracle oracle11g

我创建了一个物化视图,每5分钟刷新一次,但是当我在物化视图中插入并执行select时,我得到相同的旧数据?我需要手动刷新吗?

CREATE MATERIALIZED VIEW MVW_TEST
REFRESH FORCE ON DEMAND 
START WITH TO_DATE('01-01-2009 00:01:00', 'DD-MM-YYYY HH24:MI:SS') NEXT SYSDATE + 1/1152 
As select * from TEST12

2 个答案:

答案 0 :(得分:23)

我已逐步演示了在每个one minute之后物化视图刷新的步骤 使用5分钟后刷新的mv next(sysdate+5/1440)

第1步:

Create table temp (A int);

步骤2:

Create Materialized view temp_mv
      refresh complete start with (sysdate) next  (sysdate+1/1440) with rowid
        as select * from temp;

第3步:

select count(*) from temp;

       COUNT(*)
      ----------
          0

步骤4:

select count(*) from temp_mv;

       COUNT(*)
      ----------
          0

步骤5:

begin
      for i in 1..10 loop
         insert into temp values (i+1);
      end loop;
end;
/

Step6:

commit;

Step7:

select count(*) from temp;

       COUNT(*)
     ----------
        10

步骤8:

select count(*) from temp_mv;

       COUNT(*)
       ----------
          0

Step9:

select to_char(sysdate,'hh:mi') from dual;

       TO_CH
       -----
       04:28

步骤10:

select to_char(sysdate,'hh:mi') from dual;

       TO_CH
        -----
       04:29

步骤11:

select count(*) from temp;

      COUNT(*)
     ----------
        10

步骤12:

select count(*) from temp_mv;

      COUNT(*)
      ----------
         10

答案 1 :(得分:4)

您还可以创建调度程序作业:

BEGIN
  SYS.DBMS_SCHEDULER.CREATE_JOB
    (
       job_name        => 'DBAPER.MVW_TEST_REFRESH'
      ,start_date      => TO_TIMESTAMP_TZ('2011/09/02 00:00:00.000000 US/Central','yyyy/mm/dd hh24:mi:ss.ff tzr')
      ,repeat_interval => 'FREQ=DAILY;BYHOUR=6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22;BYMINUTE=0,5,10,15,20,25,30,35,40,45,50,55'
      ,end_date        => NULL
      ,job_class       => 'DEFAULT_JOB_CLASS'
      ,job_type        => 'PLSQL_BLOCK'
      ,job_action      => 'BEGIN 
                            DBMS_MVIEW.REFRESH(''MVW_TEST'',''C'');
                            END;'
      ,comments        => 'Job to refresh materialized view MVW_TEST.'
    );
END;
/

这样您就有了更多的配置选项,例如排除夜间或周末等。