Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Materialized view which refresh records on daily

Currently the Materialized view which I had created using REFRESH ON DEMAND so in this case I need to refresh MV explicitly using below command:

BEGIN DBMS_MVIEW.REFRESH('MV_DATA'); END; 

But now I need to refresh this MV on daily basis so could anyone please help to write this. I have seen that we can refresh this MV using writing explicit Job or using COMPLETE/FAST REFRESH statement in MV itself.

Thanks in advance!

like image 927
cool_taps Avatar asked Mar 18 '14 13:03

cool_taps


Video Answer


2 Answers

You need to create the materialized view using START WITH and NEXT Clause

create materialized view <mview_name>
refresh on demand 
start with sysdate next sysdate + 1
as select ............

So if you want to refresh mview daily, you need to keep it refresh on demand and set the next refresh time as sysdate + 1. You can set any interval although.

Once you do this the materialized view is created and a job is set in Oracle that will refresh mview every 24 hrs (sysdate + 1).

For more information on how to do that, follow this link

like image 152
San Avatar answered Oct 07 '22 18:10

San


If you simply need a SQL query to simply refresh at 12 AM, then the below query would be enough.

CREATE MATERIALIZED VIEW MV_DATA
BUILD IMMEDIATE 
REFRESH FAST START WITH (SYSDATE) NEXT (SYSDATE + 1) WITH ROWID
ON COMMIT
DISABLE QUERY REWRITE
AS SELECT * FROM <YOUR TABLE>

If you need to have it refreshed around 6 AM, then use the below script. You can see and additional logic as + 6 / 24. In case if you need to change to 4 AM, use the logic as + 4 / 24.

CREATE MATERIALIZED VIEW MV_DATA
BUILD IMMEDIATE 
REFRESH FAST START WITH (SYSDATE) NEXT (SYSDATE + 1) + 6 / 24 WITH ROWID
ON COMMIT
DISABLE QUERY REWRITE
AS SELECT * FROM <YOUR TABLE>
like image 34
Sarath KS Avatar answered Oct 07 '22 18:10

Sarath KS