Question

I need to automatically refresh a Materialized view at irregular intervals, like 0800hrs, 1200hrs 1800hrs and 2200hrs. I am only able to get as far as scheduling the refresh at regular intervals, like given below

...

REFRESH FORCE ON DEMAND START WITH sysdate+0 NEXT sysdate+(6/24)
AS Select * from Employee;
Was it helpful?

Solution

You can have a CASE statement in your NEXT. So you can do something like

NEXT (CASE WHEN to_number( to_char( sysdate, 'HH24' )) >= 22 
           THEN trunc(sysdate+1) + interval '8' hour
           ...
           ELSE null
       END)

For general sanity, I would generally create a new function (i.e. get_next_refresh_time) that implements this CASE statement and just reference the function in your materialized view.

CREATE OR REPLACE FUNCTION get_next_refresh_time 
  RETURN DATE
IS
  l_dt DATE;
BEGIN
  SELECT CASE WHEN to_number( to_char( sysdate, 'HH24' )) >= 22 
              THEN trunc(sysdate+1) + interval '8' hour
              WHEN to_number( to_char( sysdate, 'HH24' )) <  8
              THEN trunc(sysdate) + interval '8' hour
              WHEN to_number( to_char( sysdate, 'HH24' )) between 8 and 11
              THEN trunc(sysdate) + interval '12' hour
              WHEN to_number( to_char( sysdate, 'HH24' )) between 12 and 17
              THEN trunc(sysdate) + interval '18' hour
              WHEN to_number( to_char( sysdate, 'HH24' )) between 18 and 21
              THEN trunc(sysdate) + interval '22' hour
              ELSE null
          END
    INTO l_dt
    FROM dual;
  RETURN l_dt;
END;

NEXT get_next_refresh_time;

OTHER TIPS

I like to use DBMS_SCHEDULER and set up a job to call DBMS_MVIEW.REFRESH (see http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_mview.htm#ARPLS027). This has the added benefit of more options for your refresh as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top