Materialized view not refreshing - oracle

I have the following scenario:
I have created 2 tables in custom schema in 11.5.10. I have created a materialized view in a new schema(not apps) in 11.5.9 joining the 2 tables in 11.5.10
I have used following syntax for creating a MV
CREATE MATERIALIZED VIEW <new_schema>.xx_sample_testing_mv
REFRESH NEXT SYSDATE+1/1440
as
select...........
When I add new records to the 2 tables materialized view is not getting refreshed.
When I create materialized view only on a single table its getting refreshed, but not when I use more than one table.

You should check the following points:
You specified refresh period as 1 minutes(1/1440 day = 1 min). Is the materialized view refreshed after 1 minute?
Try to execute the select part and it is finished running in 1 minute.

Check are there any blocking sessions or locked objects in the database.
Check how many sessions are active and inactive for the owner of the MV using below query and if they are running for longer period then get approval from the application owner and kill the sessions and trigger the refresh again.
select sid,username,status,module,last_call_et,logon_time from v$session where username='DEMO' order by LAST_CALL_ET desc;

Related

Making sure table/materialized view created before continuing next command on Clickhouse

I'm using clickhouse-go, sometimes when I run create multiple materialized view, then query from those materialized view it shows success, but sometimes failed because the materialized view not yet created (table default.the_mv_name doesn't exists)
Is there any workaround so the db.Exec("CREATE MATERIALIZED VIEW .... POPULATE AS SELECT ...") only return when it's successfully created. Something like SELECT ... FROM bla FINAL when querying.
Or there's no workaround, so I have to loop and check system.columns table until it's ready?
for non-replicated engine - there's around 50% chance it's happened, for replicated engine - almost always happened

AUTO-Scynhronize a table based on view - ORACLE DATABASES

I want to ask you if there is a solution to auto-synchronize a table ,e.g., every one minute based on view created in oracle.
This view is using data from another table. I've created a trigger but I noticed a big slowness in database whenever a user update a column or insert a row.
Furthermore, I've tested to create a job schedule on the specified table (Which I wanted to be synchronized with the view), however we don't have the privilege to do this.
Is there any other way to keep data updated between the table and the view ?
PS : I'm using toad for oracle V 12.9.0.71
A materialized view in Oracle is a database object that contains the results of a query. They are local copies of data located remotely, or are used to create summary tables based on aggregations of a table's data. Materialized views, which store data based on remote tables, are also known as snapshots.
Example:
SQL> CREATE MATERIALIZED VIEW mv_emp_pk
REFRESH FAST START WITH SYSDATE
NEXT SYSDATE + 1/48
WITH PRIMARY KEY
AS SELECT * FROM emp#remote_db;
You can use cronjob or dbms_jobs to schedule a snapshot.

How incremental refresh on Materialized view works?

Say I have a below query which is constructing materialized view(MV)
select * from employee, department where employee.id = department.id and name like '%Andy%'
I have two related questions on How incremental update on MV works internally
1) Say I insert or update any entry either in employee or department table, can I configure increment refresh on MV to be asynchronous of insert/update transaction
or it is mandatory to be synchronous process ?
2)While insert or update , Does oracle evaluate(through transactionl logs) each updated/inserted row and apply the MV query criteria , see if satisfies it . If yes, update or insert the row in MV ?
1) Say I insert or update any entry either in employee or department table, can I configure increment refresh on MV to be asynchronous of insert/update transaction or it is mandatory to be synchronous process ?
If you want to have data available on a materialized view as soon as changes are committed on the base table then you can use ON COMMIT refresh method.
Or if you want to refresh by incrementally applying changes to the materialized view then you can use FAST refresh method. You can do this asynchronously.
2)While insert or update , Does oracle evaluate(through transactionl logas) each updated/inserted row and apply the MV query criteria , see if satisfies it . If yes, update or insert the row in MV ?
It depends upon what kind of refresh method you are using. If you are using FAST or ON COMMIT refresh method then yes, it only applies the changes to the MV which satisfy the criteria defined in the query used to create the MV.
For FAST REFRESH you have to create MATERIALIZED VIEW LOG on base tables you select for the MV. In these materialized view logs Oracle stores updated/inserted/deleted row information. i.e. Oracle does not use the REDO logs as you presumed.
Once all MViews based on your table are refreshed (either by ON COMMIT or manual REFRESH) content of according materialized view logs are truncated.

Why does a ORA-12054 error occur when creating this simple materialized view example?

ALTER TABLE RECORDINGS ADD PRIMARY KEY (ID);
CREATE MATERIALIZED VIEW LOG ON RECORDINGS TABLESPACE USERS NOLOGGING;
DROP MATERIALIZED VIEW REC_SEARCH_TEST;
CREATE MATERIALIZED VIEW REC_SEARCH_TEST
REFRESH COMPLETE ON COMMIT
AS (
SELECT DISTINCT ID, TITLE FROM RECORDINGS
);
ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view
Cannot understand what is wrong here, I know that if I take out the DISTINCT clause it works, but why can I not use 'DISTINCT' if I specify 'REFRESH COMPLETE ON COMMIT' which is required.
If I use DISTINCT and REFRESH on demand there is no problem, but these are not the requirements.
Seems like with the addition of the DISTINCT, you've made your view's underlying SQL ineligible for fast refresh, and therefore not able to be used with ON COMMIT (even tho you specify refresh complete instead of refresh fast). From Oracle docs:
The two refresh execution modes are ON COMMIT and ON DEMAND. Depending
on the materialized view you create, some of the options may not be
available. Table 8-4 describes the refresh modes.
Table 8-4 Refresh Modes
ON COMMIT
Refresh occurs automatically when a transaction that modified one of
the materialized view's detail tables commits. This can be specified
as long as the materialized view is fast refreshable (in other words,
not complex). The ON COMMIT privilege is necessary to use this mode.
ON DEMAND
Refresh occurs when a user manually executes one of the available
refresh procedures contained in the DBMS_MVIEW package (REFRESH,
REFRESH_ALL_MVIEWS, REFRESH_DEPENDENT).
The same document link has a list of restrictions for fast refresh as well.
"Perhaps the example isn't the best, because I want to expand the view
to a more complicated query that will require a distinct keyword,
right now I am just trying to get it working on a basic level. "
The DISTINCT is the cause of the ORA-12054.
SQL> CREATE MATERIALIZED VIEW REC_SEARCH_TEST
REFRESH COMPLETE ON COMMIT
AS (
SELECT DISTINCT empno, ename FROM emp
)
/
2 3 4 5 6
SELECT DISTINCT empno, ename FROM emp
*
ERROR at line 4:
ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view
Elapsed: 00:00:01.14
SQL> SQL>
SQL> CREATE MATERIALIZED VIEW REC_SEARCH_TEST
REFRESH COMPLETE ON COMMIT
AS (
SELECT empno, ename FROM emp
)
/
2 3 4 5 6
Materialized view created.
Elapsed: 00:00:02.33
SQL>
Why not start with a something that works? Remove the DISTINCT. Get your MView working. Then complicate it later when it becomes necessary.
Although, as you already know you cannot use a DISTINCT you will have to revise either your query's logic or your refresh strategy.
The important thing to note about the question is that it is not about fast refresh, but complete refresh. Thus, there is no logical reason that the usual restrictions for fast refresh on commit should apply, except the one that all referenced objects must be local.
The "refresh complete on commit is a relatively new feature, so the best answer to the "why" question is probably "Oracle has not yet implemented this fully, please check future versions of Oracle Database". Not very useful, but true...

Refreshing Materialzed View in Oracle

I have created materialized view (MV) on a table which is updated once in a month..
Do we have any automatic way to refresh my MV...
I mean how refresh of MV is done is it manual or
Automatic how frequent we can do????
like can i use Trigger for it..??
I am using Oracle9i on PL/SQL developer
Thanks
You can refresh the view manually if you want
execute DBMS_SNAPSHOT.REFRESH( 'MAT_VIEW','OPTION');
Where the OPTION parameter could be
F, f Fast Refresh
C, c Complete Refresh
A Always perform complete refresh
? Use the default option
The automatic refresh rate are supplied when you create the materialized view
CREATE MATERIALIZED VIEW MAT_VIEW
REFRESH FAST START WITH SYSDATE
NEXT SYSDATE + TIME_INTERVAL --
WITH PRIMARY KEY
AS SELECT * FROM TABLE;
Remember to create a log for the table if FAST START are used.
I recomend you the following link about Materialized View. It has a lot of info about this topic: http://www.dba-oracle.com/art_9i_mv.htm

Resources