Which DB my mview resides???

Today one of my friend called me and asked –

Can we determine the database and the mview, from the database where mview log resides?

The mview log was occupying lot of space which he wanted to drop and recreate, which was followed by complete refresh of mview (which used that mview log).He had no idea which database and which mviews were using that mview log.

So, here is how we can determine the mview and database name.

Session1
==========
Created mview log on scott.emp table in MATRIX database

23:34:55 SCOTT@MATRIX> SELECT master, log_table, rowids, primary_key FROM user_snapshot_logs;

no rows selected

Elapsed: 00:00:00.01
23:35:01 SCOTT@MATRIX> create materialized view log on emp;

Materialized view log created.

Elapsed: 00:00:00.06
23:35:04 SCOTT@MATRIX> SELECT master, log_table, rowids, primary_key FROM user_snapshot_logs;

MASTER                         LOG_TABLE                      ROW PRI
------------------------------ ------------------------------ --- ---
EMP                            MLOG$_EMP                      NO  YES

Elapsed: 00:00:00.01

Session 2
===========
Logged in to another database ORCL, create a public database link to scott@MATRIX

23:36:20 SYS@ORCL> create public database link mv_dblink connect to scott identified by tiger using 'MATRIX';

Database link created.

Elapsed: 00:00:00.01
23:36:31 SYS@ORCL>
23:36:31 SYS@ORCL>
23:36:31 SYS@ORCL> select * from dual@mv_dblink;

D
-
X

Elapsed: 00:00:00.12

Session 2
============

Created fast refresh mview (emp_mv) in ANAND schema – ORCL db

23:36:39 SYS@ORCL> conn anand/anand123
Connected.
23:36:49 ANAND@ORCL>
23:36:50 ANAND@ORCL>
23:36:50 ANAND@ORCL>
23:36:50 ANAND@ORCL>
23:36:50 ANAND@ORCL>
23:36:50 ANAND@ORCL> create materialized view emp_mv
23:37:14   2  build immediate
23:37:21   3  refresh fast
23:37:27   4  as
23:37:31   5  select * from emp@mv_dblink;

Materialized view created.

Elapsed: 00:00:01.25
23:37:53 ANAND@ORCL>
23:37:53 ANAND@ORCL>
23:37:54 ANAND@ORCL>
23:37:54 ANAND@ORCL> select * from emp_mv;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

14 rows selected.

Elapsed: 00:00:00.09

Now suppose you are not aware of the mview and you are asked to determine the mview and the database it resides in, from the database where mview log exists, how will you do it??

Session 1
==========

23:44:47 SYS@MATRIX> SELECT r.NAME mview_name, snapid, NVL(r.mview_site, 'not registered') mview_site, snaptime FROM   sys.slog$ s, dba_registered_mviews r WHERE  s.snapid=r.mview_id(+) AND mowner LIKE UPPER('&owner') AND MASTER LIKE UPPER('&table_name');
Enter value for owner: scott
Enter value for table_name: emp
old   1: SELECT r.NAME mview_name, snapid, NVL(r.mview_site, 'not registered') mview_site, snaptime FROM   sys.slog$ s, dba_registered_mviews r WHERE  s.snapid=r.mview_id(+) AND mowner LIKE UPPER('&owner') AND MASTER LIKE UPPER('&table_name')
new   1: SELECT r.NAME mview_name, snapid, NVL(r.mview_site, 'not registered') mview_site, snaptime FROM   sys.slog$ s, dba_registered_mviews r WHERE  s.snapid=r.mview_id(+) AND mowner LIKE UPPER('scott') AND MASTER LIKE UPPER('emp')

MVIEW_NAME                         SNAPID MVIEW_SITE           SNAPTIME
------------------------------ ---------- -------------------- ---------
EMP_MV                                 42 ORCL                 01-FEB-11

Elapsed: 00:00:00.01
23:44:50 SYS@MATRIX>

In the above query, you need to provide the schema name and table name for which mview log is created as input.The output is the mview name and the database name where it exists.

Hope this helps 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s