SlideShare a Scribd company logo
1 of 85
Download to read offline
Dr. Girija Narasimhan
PART 1
View
Vs.
Materialized view
OER CHAPTER 2
Using materialized views, it is possible to pre calculate
complex joins and execute the aggregate operation i.e
summary data and also stores the result set of the table
in the database.
In future the end users may query the table and views
the detail information.
The query rewrite mechanism in the Oracle server
automatically rewrites the SQL query to use the summary
tables.
This technique improves the query execution time and
performance.
Dr. Girija Narasimhan
Dr. Girija Narasimhan
create table item(itemno number(3) primary key
,iname varchar2(10),price number(4));
insert into item values(1,'milk',20);
insert into item values(2,'Bread',30);
insert into item values(3,'Juice',40);
Table Materialized View
select itemno,Iname from
item ;
create materialized view mv as
select itemno,iname from item;
select * from MV ;
ITEMNO INAME
------ -----
1 milk
2 Bread
3 Juice
ITEMNO INAME
------- -----
1 milk
2 Bread
3 Juice
Dr. Girija Narasimhan
•A materialized view is a database object.
• It is physically stored in the database.
•Because materialized view tables are having local copies
of data suppose base table is located remotely.
•It is also useful to create summary table based on
aggregation of the base table data.
•The materialized view stores both query and the result
of a query.
Dr. Girija Narasimhan
A view is a logical entity or virtual table.
The view is attached with SQL statement or query, which will store in the
database in the system table space. It is used as similar like database table or
base table (i.e item table).
Whenever query is fired against the database table or base table (i.e. item table),
The view “v” execute the stored SQL statement ( i.e. select * from item) and
creates a temporary table in the memory.
It do not stores the query result, it only execute and return the result /output. It
means it will store only query not result.
Dr. Girija Narasimhan
Both base table and view table are identical, since both the base table and view
table “Rowid’s” are same.
Rowid is the physical address for each record in the database and it is a fixed-
length binary data.
So, it returns the same value and also any one table is updated the other table
automatically returns the exact same result.
Base table and view table Rowid’s are same
Dr. Girija Narasimhan
Whenever update the base table (item table), the view also automatically update the change.
Otherwise update the view (“v”), the base table (item table) also automatically modifies the
changes. For example after the update, the view data matches the table data but the
materialized view data does not.
Dr. Girija Narasimhan
The reason for materialized view is not reflecting the updation of base table like view
is, materialized view storing the copy of data in a separate physically place in the
database.
The given below table clearly shows base table rowid and materialized view rowid are
not same.
Dr. Girija Narasimhan
The reason for materialized view is not reflecting the
updation of base table is, materialized view storing the
copy of data in a separate physically place in the
database. i.e. both have different Rowid
Rowid is the physical address for each record in the
database and it is a fixed-length binary data
Table Materialized View
update item set iname = upper(iname);
select itemno,iname from item ; select * from MV ;
ITEMNO INAME
---------- ----------
1 MILK
2 BREAD
3 JUICE
ITEMNO INAME
------- -----
1 milk
2 Bread
3 Juice
Dr. Girija Narasimhan
To apply
refresh technique
to
materialized view table
for synchronize With
base table data.
Refresh complete,
Refresh Fast,
Refresh Force
Dr. Girija Narasimhan
Mode of Refresh Technique
PART 2
Dr. Girija Narasimhan
ON DEMAND ON COMMIT
Mode of Refresh
By default mode, it
need manual refresh
execution procedure
Automatic refresh,
no need of execute
refresh procedure
Dr. Girija Narasimhan
There is two mode of refresh technique
is available, one is “ON DEMAND” and
other one is “ON COMMIT”.
create materialized view
mv as select itemno,iname
from item;
=
create materialized view mv
REFRESH ON DEMAND as
select itemno,iname from
item;
Table Materialized View
update item set iname = upper(iname);
select itemno,iname from item ; select * from MV ;
ITEMNO INAME
---------- ----------
1 MILK
2 BREAD
3 JUICE
ITEMNO INAME
------- -----
1 milk
2 Bread
3 Juice
Dr. Girija Narasimhan
For matching updating of data in materialized view table (mv) and base
table (item), the refresh procedures are available in the DBMS_MVIEW
package.
When execute the refresh procedure, the materialized view table (mv)
synchronized with base table (item)
Table Materialized View
select * from item; select itemno,iname from MV ;
ITEMNO INAME
------ ----------
1 MILK
2 BREAD
3 JUICE
ITEMNO INAME
------ --------
1 MILK
2 BREAD
3 JUICE
SQL> execute dbms_mview.refresh( 'MV' );
PL/SQL procedure successfully completed.
Dr. Girija Narasimhan
The other method is called “ON COMMIT”, Unlike manual
refresh method, it doesn’t need “DBMS_MVIEW.REFRESH” to
execute.
Create materialized view mv on commit as select * from
item;
Select itemno,iname from mv
ITEMNO INAME
--------- ---------
1 Fruits
2 COFFEE
8 Biscutts
5 rice
Dr. Girija Narasimhan
select * from item;
ITEMNO INAME
---------- ----------
3 Water
1 Fruits
2 COFFEE
8 Biscutts
5 rice
Select itemno,iname from mv;
ITEMNO INAME
------- -----------
1 Fruits
2 COFFEE
8 Biscutts
5 rice
3 Water
Insert into item values(3,’Water’)
Commit;
It automatically refreshed, i.e it is synchronized with
base table data just by “COMMIT” statement.
Dr. Girija Narasimhan
Introduction
Of
Refresh option
PART 3
Dr. Girija Narasimhan
Create Materialized view using
Refresh options
Refresh complete Refresh Fast Refresh Force Never Refresh
Method =‘C’
execute
Method =‘F’
It create new result set,
with new row id
No Need to mention
method “F” in the Execute
statement.
It don’t create new result
set in different row id,
same existing row id
User specifying saying Method
either ‘C’ or ‘F’
Method =>’C’ Method =>’F’ Method =>’ ?’
System decides based on
situation it will automatically
assign either ‘C’ or ‘F’
ALTER MV into refresh
complete, refresh fast,
refresh force
Execute statement must be
method =>’C’
otherwise error occur
Dr. Girija Narasimhan
Refresh Complete
PART 5
Dr. Girija Narasimhan
When a refresh complete occurs in the materialized view's defining query
is executed and the complete result set replaces the data currently existed
in the materialized view.
As a result, it completely re-creates the result set of the materialized view
Before Execute After Execute using “C” method
select rowid,itemno,iname from mv2;
execute DBMS_MVIEW.REFRESH(LIST =>
'MV2', method=>'c');
select rowid,itemno,iname from mv2;
ROWID ITEMNO INAME
AAAMEQAAEAAAAB0AAA 1 MILK
AAAMEQAAEAAAAB0AAB 2 BREAD
AAAMEQAAEAAAAB0AAC 3 JUICE
ROWID ITEMNO INAME-
AAAMEQAAEAAAAB0AAD 1 MILK
AAAMEQAAEAAAAB0AAE 2 BREAD
AAAMEQAAEAAAAB0AAF 3 JUICE
SQL> create materialized view mv2 refresh complete as
select * from item;
The rowids after execute differs from the before execute, even
though the data in base table ITEM was unchanged
Dr. Girija Narasimhan
execute DBMS_MVIEW.REFRESH(LIST => 'MV2', METHOD => 'C' );
The "list" parameter accepts a list of materialized views to refresh (i.e
“mv2”) and the "method" parameter accepts a "C", for Complete
refresh. (i.e ‘C’ means Complete refresh)
The limitation of refresh complete is , suppose if a materialized view
have many rows and the base tables values are changed infrequently
then refresh complete is time-consuming method or over slow.
Another issue is due to bad connections sometimes the refresh
completes never finishing their process.
Dr. Girija Narasimhan
execute DBMS_MVIEW.REFRESH(LIST => 'MV2', METHOD =>'F' );
create materialized view mv2 refresh complete as select * from item;
Before Execute After Execute using “F” method
select rowid,itemno,iname from mv2;
execute DBMS_MVIEW.REFRESH(LIST => 'MV2',
method=>'F');
select rowid,itemno,iname from mv2;
ROWID ITEMNO INAME
AAAMEQAAEAAAAB0AAA 1 MILK
AAAMEQAAEAAAAB0AAB 2 BREAD
AAAMEQAAEAAAAB0AAC 3 JUICE
ROWID ITEMNO INAME
AAAMEQAAEAAAAB0AAA 1 MILK
AAAMEQAAEAAAAB0AAB 2 BREAD
AAAMEQAAEAAAAB0AAC 3 JUICE
Even use “Refresh complete” in the materialized view creation, even
though call the method “F” it will do “refresh fast”.
The notable point is “Rowid” before and after executing refresh is not
changed.
Dr. Girija Narasimhan
PART 4
Refresh Fast
Dr. Girija Narasimhan
REFRESH FAST clause used in the CREATE MATERIALIZED VIEW
command tells the oracle suppose no need to mention refresh
option in the execution.
The above statement is not mentioning any “method =>’F’, but
create materialized view “mv” is using refresh fast. So, the default
refresh method in the execute statement refresh fast only
create materialized view MV refresh fast as
select * from item;
Execute dbms_mview.refresh( 'MV' );
Dr. Girija Narasimhan
create materialized view mv refresh fast as select * from item;
Before Execute After Execute using “F” method
select rowid,itemno,iname from mv;
execute DBMS_MVIEW.REFRESH(LIST =>
'MV', method=>'F');
select rowid,itemno,iname from mv;
ROWID ITEMNO INAME
AAAMEQAAEAAAAB0AAA 1 MILK
AAAMEQAAEAAAAB0AAB 2 BREAD
AAAMEQAAEAAAAB0AAC 3 JUICE
ROWID ITEMNO INAME
AAAMEQAAEAAAAB0AAA 1 MILK
AAAMEQAAEAAAAB0AAB 2 BREAD
AAAMEQAAEAAAAB0AAC 3 JUICE
update item set iname='JAM' where itemno=3
execute DBMS_MVIEW.REFRESH('MV’');
select rowid,itemno,iname from mv;
ROWID ITEMNO INAME
-----------------------------------------------------------------------------------------------------
AAAMEQAAEAAAAB0AAA 1 MILK
AAAMEQAAEAAAAB0AAB 2 BREAD
AAAMEQAAEAAAAB0AAC 3 JAM
The benefit of using REFRESH FAST is, it don’t create entire new result set using
new rowid like REFRESH COMPLETE.
The value was updated in the materialized view without changing the “Rowid”.
Dr. Girija Narasimhan
PART 5
Refresh Force
Dr. Girija Narasimhan
“REFRESH FORCE” clause to decide which performance is most
appropriate for query rewrite using DML operation either refresh
fast or refresh complete based on the Operation.
Create materialized view mv3 refresh force as select max(itemno) as max from item;
SQL> select rowid,max from mv3;
ROWID MAX
-----------------------------------------------------------------------------------
AAAMFEAAEAAAACMAAA 6
update item set iname='jam' where itemno=6;
execute dbms_mview.refresh('mv3');
SQL> select rowid,max from mv3;
ROWID MAX
--------------------------------------- ----------
AAAMFEAAEAAAACMAAB 6
the update operation in the materialized and after execute it changes
the rowid; here refresh force performs the refresh complete method.
Dr. Girija Narasimhan
Create materialized view mv3 refresh force as select itemno,iname from item
where itemno>4;
SQL> select rowid,itemno,iname from mv3;
ROWID ITEMNO INAME
----------------------------------- --------- ----------
AAAMFGAAEAAAACMAAA 5 water
AAAMFGAAEAAAACMAAB 6 jam
update item set iname='pepsi' where itemno=5;
execute dbms_mview.refresh('mv3');
SQL> select rowid,itemno,iname from mv3;
ROWID ITEMNO INAME
------------------------------------------------------------ ------------------------------
AAAMFGAAEAAAACMAAA 5 pepsi
AAAMFGAAEAAAACMAAB 6 jam
update operation are executed, here Rowid is not changing. The
refresh force is performing the “Refresh fast”
Dr. Girija Narasimhan
PART 6
Never Refresh
using
ALTER Refresh complete
Dr. Girija Narasimhan
Oracle Database will ignore any REFRESH statement on the
materialized view and prevents any refresh method such as FAST or
COMPLETE by using NEVER REFRESH in create materialized
view statement.
Create materialized view mv3 never refresh as select itemno,iname from item where
itemno>4;
SQL> select rowid,itemno,iname from mv3;
ROWID ITEMNO INAME
-------------------------------------- ---------- ----------
AAAMFJAAEAAAACMAAA 5 pepsi
AAAMFJAAEAAAACMAAB 6 jam
SQL> update item set iname='water' where itemno=5;
SQL> execute dbms_mview.refresh('mv3');
BEGIN dbms_mview.refresh('mv3'); END;
*ERROR at line 1:ORA-23538: cannot explicitly refresh a NEVER REFRESH
materialized view ("MV3")
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 1883
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 2089
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 2058
Dr. Girija Narasimhan
Alter the materialized view into refresh complete and then execute the
materialized view.
Now, I don’t give error message.
alter materialized view mv3 refresh complete;
execute dbms_mview.refresh('mv3');
SQL> select rowid,itemno,iname from mv3;
ROWID ITEMNO INAME
-------------------------------------------------------------------------------------------
AAAMFJAAEAAAACMAAC 5 water
AAAMFJAAEAAAACMAAD 6 jam
Dr. Girija Narasimhan
PART 7
Never Refresh
using
ALTER Refresh Fast
Dr. Girija Narasimhan
Instead of altering materialized view into refresh complete, alter
into refresh fast.
Then try any DML operations, it gives the error.
The reason is, it alters the materialized view into “read-only"
type.
So, it doesn’t allow any DML operation.
In this case, call “refresh complete” for execution. Complete
refresh creates the entirely new set.
Dr. Girija Narasimhan
alter materialized view mv3 refresh fast;
execute dbms_mview.refresh('mv3');
Insert into item values(8,’Biscutts’)
BEGIN dbms_mview.refresh('mv3'); END;*ERROR at line 1:
ORA-12057: materialized view "SCOTT"."MV3" is INVALID and must complete
refresh
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 1883
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 2089
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 2058
ORA-06512: at line 1
execute dbms_mview.refresh( list => 'MV3',method=>'c');
SQL> select rowid,itemno,iname from mv3;
ROWID ITEMNO INAME
------------------ ---------- ----------
AAAMFJAAEAAAACMAAE 5 water
AAAMFJAAEAAAACMAAF 6 jam
AAAMFJAAEAAAACMAAG 8 Biscutts
Dr. Girija Narasimhan
PART 8
Never Refresh
using
ALTER Refresh Force
Dr. Girija Narasimhan
Create materialized view mv3 never refresh as select itemno,iname from item where
itemno>4;
alter materialized view mv3 refresh force;
select rowid,itemno,iname from mv3;
ROWID ITEMNO INAME
----------------------------------- ---------- ----------
AAAMFPAAEAAAACMAAA 5 Lemon
AAAMFPAAEAAAACMAAB 8 Biscutts
update item set iname='rice' where itemno=5;
SQL> execute DBMS_MVIEW.REFRESH(LIST => 'MV3',method=>'?');
SQL> select rowid,itemno,iname from mv3;
ROWID ITEMNO INAME
------------------------------------- ---------- ----------
AAAMFPAAEAAAACMAAC 5 rice
AAAMFPAAEAAAACMAAD 8 Biscutts
Dr. Girija Narasimhan
Alter the materialized view from never fresh into refresh force,
now execute the update operation.
In the execute statement, the method “?” means refresh force.
But refresh force applies only refresh complete method, not
refresh fast.
It shows in the result set that “rowid” has changed
Dr. Girija Narasimhan
PART 9
Create Materialized view Log
MLOG$
Dr. Girija Narasimhan
As seen earlier topic, complete refresh is time-consuming and
occupying storage space.
These drawbacks are rectified by applying Materialized View
Log
The materialized view captures only the changed rows occur in
the base table, it don’t captures the entire value.
This technique is called Materialized View Log and
“incremental" refreshing
Usually, a Materialized View Log takes less time than a
complete refresh.
Dr. Girija Narasimhan
DESC ITEM;
Name Null? Type
----------------------------------------- -------- ----------------------------
ITEMNO NOT NULL NUMBER(3)
INAME VARCHAR2(10)
create materialized view log on item;
In this materialized view log, the name for materialized view is not
given name.
For example in the previous materialized view used “mv”, “mv2" like
that.
Because a base table has only one materialized view log related at
a time, so a separate materialized view name is not required.
Dr. Girija Narasimhan
describe MLOG$_item;
Name Null? Type
----------------------------------------- -------- ----------------------------
ITEMNO NUMBER(3)
SNAPTIME$$ DATE
DMLTYPE$$ VARCHAR2(1)
OLD_NEW$$ VARCHAR2(1)
CHANGE_VECTOR$$ RAW(255)
Once materialized view log was created, it automatically generates a log
file called MLOG$_<base table>
The MLOG$_item.itemno column copy the base table’s primary key
column item.itemno.
The MLOG$ also uses in the primary key column data type and size is as
same as base table item i.e data type is NUMBER(3).
The other MLOG$ columns are system generated for example
SNAPTIME$$, DMLTYPE$$,OLDNEW$$,CHANGE_VECTOR$$.
Dr. Girija Narasimhan
PART 10
Materialized view Log
DMLTYPE$$
Dr. Girija Narasimhan
UPDATE ITEM set INAME=‘COFFEE’ where ITEMNO = 2 ;
INSERT into ITEM(ITEMNO,INAME)values (4,’Biscutts’);
select itemno, dmltype$$ as dmltype from MLOG$_item;
ITEMNO D
--------------------
2 U
4 I
select * from MLOG$_ITEM;
no rows selected
Materialized view log is initially empty.
Rows are automatically added to MLOG$_ITEM when base table ITEM is executed
by any DML operation such that UPDATE, DELETE or INSERT.
The result of the MLOG$_item, shows number of changes occur in the base table.
Here “U” – update operation in itemno 2 and “I”- insert operation occurred in itemno
4 in the base table item.
Dr. Girija Narasimhan
rollback ;
select itemno, dmltype$$ from MLOG$_item;
no rows selected
Suppose, if rollback all the DML operation executed in the base table, the
MLOG$_ITEM is again empty.
The changes are not committed in the base table item.
The materialized view log is using “on commit” by
default. That is a reason it don’t need execute any
“DBMS_MVIEW.REFRESH” procedure to initiate the
refresh in the materialized view.
Dr. Girija Narasimhan
PART 11
Materialized view Log
WITH Clause
summary
Dr. Girija Narasimhan
Materialized view Log
With
Primary Key Row id Column name Sequence$$
By default
mlog$ have
primary key
column.
It will take data
type and size of
the base table.
It will create M_ROW$$
column in the Mlog$
Its data type and size is
VARCHAR2(255).
It will replace the primary
key column in the Mlog$
Suppose newly creating
mlog$ with primary
key,rowid then mlog$
have both primary key and
rowid.
Already created mlog$ use
add rowid
The WITH clause
can also contain a
list of specific base
table columns.
it will use data
type and size of
column is as
mentioned in the
base table
Using “WITH
SEQUENCE” in the
materialized view log
statement includes
SEQUENCE$$ column
in MLOG$ table.
This SEQUENCE$$
column is help oracle
to keep storing exact
ordering information
of mixed combination
of DML operations
used in the multiple
base table.
Dr. Girija Narasimhan
PART 12
Materialized view Log
WITH Clause
Primary Key
Dr. Girija Narasimhan
Any way by default, the MLOG$ includes the primary key of the base table.
To include the base table's primary key column in a materialized view log the
WITH PRIMARY KEY clause can be specified.
create materialized view log on item WITH PRIMARY KEY
=
create materialized view log on item
Dr. Girija Narasimhan
DESC ITEM;
Name Null? Type
----------------------------------------- -------- ----------------------------
ITEMNO NOT NULL NUMBER(3)
INAME VARCHAR2(10)
drop materialized view log on item ;
create materialized view log on item WITH PRIMARY KEY ;
desc mlog$_item;
Name Null? Type
---------------------------------------------------------------
ITEMNO NUMBER(3)
SNAPTIME$$ DATE
DMLTYPE$$ VARCHAR2(1)
OLD_NEW$$ VARCHAR2(1)
CHANGE_VECTOR$$ RAW(255)
Dr. Girija Narasimhan
PART 13
Materialized view Log
WITH Clause
ROWID
Dr. Girija Narasimhan
ROWID to be specified in the materialized view log using
“with” clause
drop materialized view log on item ;create materialized view log on
item WITH ROWID ;
desc mlog$_item;
Name Null? Type -----------------------------------
--------- -------- -------
M_ROW$$ VARCHAR2(255)
SNAPTIME$$ DATE
DMLTYPE$$ VARCHAR2(1)
OLD_NEW$$ VARCHAR2(1)
CHANGE_VECTOR$$ RAW(255)
Using “WITH” clause include the ROWID in the materialized log
view, instead of primary key column ITEMNO it is substituted
by M_ROW$$ column.
Dr. Girija Narasimhan
drop materialized view log on item;
create materialized view log on item WITH ROWID, PRIMARY KEY;
desc mlog$_item
Name Null? Type
----------------------------- -------- ----------------------------
ITEMNO NUMBER(3)
M_ROW$$ VARCHAR2(255)
SNAPTIME$$ DATE
DMLTYPE$$ VARCHAR2(1)
OLD_NEW$$ VARCHAR2(1)
CHANGE_VECTOR$$ RAW(255)
It is also possible to include both rowid and primary key
in MLOG$ table, by using WITH clause,
Dr. Girija Narasimhan
SQL> ALTER MATERIALIZED VIEW LOG ON item ADD ROWID;
SQL> DESC MLOG$_ITEM;
Name Null? Type
--------------------------------------------------------
ITEMNO NUMBER(3)
SNAPTIME$$ DATE
DMLTYPE$$ VARCHAR2(1)
OLD_NEW$$ VARCHAR2(1)
CHANGE_VECTOR$$ RAW(255)
M_ROW$$ VARCHAR2(255)
Another alternative method for include rowid and primary in the
MLOG$ table without dropping the existing MLOG$ table.
In other words, say that there is no need to recreate MLOG$ table.
Dr. Girija Narasimhan
PART 14
Materialized view Log
WITH Clause
COLUMN NAME
Dr. Girija Narasimhan
The WITH clause can also contain a list of specific base table
columns.
For example include the Iname column from the base table.
Here, it will use data type and size of column is as mentioned in
the base table such as “VARCHAR2(10)”.
create materialized view log on item WITH (iname);
SQL> desc mlog$_item;
Name Null? Type
------------------------------------ -------- --------------
ITEMNO NUMBER(3)
INAME VARCHAR2(10)
SNAPTIME$$ DATE
DMLTYPE$$ VARCHAR2(1)
OLD_NEW$$ VARCHAR2(1)
CHANGE_VECTOR$$ RAW(255)
55
Dr. Girija Narasimhan
PART 15
Materialized view Log
WITH Clause
SEQUENCE$$
Dr. Girija Narasimhan
Using “WITH SEQUENCE” in the materialized view log statement
includes SEQUENCE$$ column in MLOG$ table.
create materialized view log on ITEM WITH SEQUENCE ;
desc mlog$_item;
Name Null? Type
-------------------------------------------------------------------------------------
ITEMNO NUMBER(3)
SEQUENCE$$ NUMBER
SNAPTIME$$ DATE
DMLTYPE$$ VARCHAR2(1)
OLD_NEW$$ VARCHAR2(1)
CHANGE_VECTOR$$ RAW(255)
This SEQUENCE$$ column is help oracle to keep storing exact ordering
information of mixed combination of DML operations used in the multiple base
table.
For example insert, update and delete operations are executed on multiple base
tables in different order.
Dr. Girija Narasimhan
drop materialized view log on ITEM ;
create materialized view log on ITEM WITH SEQUENCE ;
create materialized view log on SUPPLIER WITH SEQUENCE ;
INSERT into ITEM values ( 6, 'Chocolate' );
INSERT into supplier values (4,’Rasna’);
delete from item where itemno=4;
UPDATE supplier set sname = ‘AVT Tea’ where suppno=3 ;
commit;
select SEQUENCE$$, itemno,
dmltype$$ from mlog$_item;
select SEQUENCE$$, suppno,
dmltype$$ from mlog$_supplier
;
SEQUENCE$$ itemno DMLtype$$
---------- ---------- -------------------------
1 6 I
3 4 D
SEQUENCE$$ SUPPNO dmltype$$
---------- ---------- - ----------------------------
2 4 I
4 3 U
Dr. Girija Narasimhan
PART 16
Materialized view Log
OLD_NEW$$
Dr. Girija Narasimhan
The OLD_NEW$$ column in the MLOG$ stores the values
before updating the base table value.
It is helpful in future for verifying the previous value in the
same column and also indicates the base table values were
refreshed.
UPDATE item set iname= 'water' where itemno = 3;
select itemno, iname, old_new$$ from mlog$_item;
ITEMNO INAME OLD_NEW$$
---------- ---------- ----------
3 JUICE O
Dr. Girija Narasimhan
PART 17
Materialized view Log
INCLUDING NEW VALUES
Dr. Girija Narasimhan
By default the materialized view log exclude the new value.
Specify EXCLUDING to disable the recording of new values in the
log. In some situations, it helps to identify both the old value and
the new value explicitly saved in the materialized view log.
INCLUDING NEW VALUES tells the oracle database to save old and
new values in OLD_NEW$$ column for update DML operations in
the materialized view log
create materialized view log on ITEM with sequence (INAME )
INCLUDING NEW VALUES ;
update ITEM set INAME = 'Fruits' where ITEMNO = 1 ;
select sequence$$, itemno, iname, old_new$$ from mlog$_item
order by sequence$$;
SEQUENCE$$ ITEMNO INAME OLD_NEW$$
-----------------------------------------------------------------------
5 1 MILK O
6 1 Fruits N
Dr. Girija Narasimhan
There is no need to store the new value for an
update because it can be derived by applying
the change vector (a RAW value stored in
CHANGE_VECTOR$$, which Oracle uses
internally during refreshes) to the old value.
Dr. Girija Narasimhan
PART 18
COMMENTS
ON
Materialized view
Dr. Girija Narasimhan
It is possible to add comment in materialized view.
This comment statement added into the data dictionary for the
already existing materialized view table.
The comment table has three columns in the data dictionary regarding
the materialized view:
Owner - owner of the materialized view
Mview_name - materialized view name
Comments -Comment on the materialized view
SQL> comment on materialized view mv is 'ITEM Information';
Comment created.
SELECT MVIEW_NAME, COMMENTS FROM USER_MVIEW_COMMENTS WHERE
MVIEW_NAME = ‘MV';
MVIEW_NAME COMMENTS
------------------------------------------------
MV ITEM Information
Dr. Girija Narasimhan
There is three types of comment view are available in the data
dictionary for materialized view.
•USER_MVIEW_COMMENTS display the current user comments
on the materialized views. This view does not display the OWNER
column. It will display only Mview_name, comments column.
•DBA_MVIEW_COMMENTS displays the database comments on
the materialized views. It will display all the three columns such
as owner, mview_name, comments.
•ALL_MVIEW_COMMENTS displays all the current user comments
on the materialized view. It will display all the three columns such
as owner, mview_name, comments.
Dr. Girija Narasimhan
SELECT OWNER, MVIEW_NAME, COMMENTS FROM
ALL_MVIEW_COMMENTS;
OWNER MVIEW_NAME COMMENTS
----------------------------------------------------------
SCOTT MV1 Snapshot table for snapshot SCOTT.MV1
SCOTT MV ITEM Information
SCOTT MV2 ITEM DESCRIPTION
Drop comments on materialized view
SQL>comment on materialized view mv is ' ';
Syntax:
Comment on materialized view <name of the materialized
view> is ‘< within single code give blank space>‘;
Dr. Girija Narasimhan
PART 19
Materialized view
Using
“For Update”
Dr. Girija Narasimhan
By default materialized view tables are read-only.
Suppose tried with DML operation in the materialized view table causes the
error message
SQL> INSERT into mv(ITEMNO,INAME)values (5,'Chocolate');
INSERT into mv(ITEMNO,INAME)values (5,'Chocolate')
*
ERROR at line 1:
ORA-01732: data manipulation operation not legal on this view
Suppose materialized view is created using FOR UPDATE clause, then it enables
DML operations directly in the materialized view.
This type of materialized view is called updatable materialized view.
Dr. Girija Narasimhan
SQL> drop materialized view mv;
SQL> create materialized view mv for update as select
itemno,iname from item;
SQL> select itemno,iname from
item;
ITEMNO INAME
---------- ----------
1 MILK
2 COFFEE
3 JUICE
4 Biscutts
SQL> select itemno,iname from
mv;
ITEMNO INAME
---------- ----------
1 MILK
2 COFFEE
3 JUICE
4 Biscutts
The data in the materialized view table “mv” and base table “item” are identical.
Now insert one record only in materialized view not in the base table “item”
SQL> INSERT into mv(ITEMNO,INAME)values (5,'Chocolate');
SQL> select itemno,iname from mv;
ITEMNO INAME
---------- ----------
1 MILK
2 COFFEE
3 JUICE
4 Biscutts
5 Chocolate
SQL> select itemno,iname
from item;
ITEMNO INAME
---------- ----------
1 MILK
2 COFFEE
3 JUICE
4 Biscutts
Dr. Girija Narasimhan
The materialized view table “mv” is not matching with “item” base table.
Because materialized view stores the copy of the result i.e ROWID is different
than ROWID of the item table.
So, it is not matching with base table result.
To overcome this difficulty, it is necessary to execute refresh method in
materialized view.
Dr. Girija Narasimhan
PART 20
Materialized view
Using
Aggregate Function
In the data warehousing environment the
materialized views normally include aggregates.
In the materialized view containing aggregates is
possible after any type of DML to the base
tables. In this example using MIN, MAX, SUM,
AVG, COUNT aggregate function.
Dr. Girija Narasimhan
PART 21
Join Materialized
view
The Join has two tables. In the example used two tables
namely Item and Supplier. The row in the one table always
match with row of the another table. Here, no data will be
lost i.e it is called lossless join. The table should have
Primary key, Foreign Key and Not Null constraints on
appropriate Join keys.
Dr. Girija Narasimhan
PART 22
Materialized view
Status
Whenever materialized view and base table data’s are
synchronized then it is considered that materialized view has
fresh data otherwise it has the stale data (out-of-date data).
For knowing the status of materialized view data, there is
three types of view are available namely DBA_MVEIWS,
ALL_MVIEWS, and USER_MVIEWS in the data dictionary.
In the data dictionary the columns of the all the three views
such as MVIEW_NAEM, STALNESS, LAST_REF,
COMPILE_STATE are status are maintain automatically.
The STALENESS column display any one value like FRESH, STALE,
UNUSABLE, UNKNOWN, UNDEFINED or NEEDS_COMPILE.
Whenever NEEDS_COMPILE in these views shows NEEDS_COMPILE
OR VALID status. Suppose the STALENESS column value has
NEEDS_COMPILE then issue following alter materialized view
statement for compile.
Dr. Girija Narasimhan
A materialized view is a database object. It is physically
stored in the database.
Because materialized view tables are having local copies
of data suppose base table is located remotely.
It is also useful to create summary table based on
aggregation of the base table data.
The materialized view stores both query and the result
of a query.
Dr. Girija Narasimhan
PART 23
Materialized view Build Methods
Dr. Girija Narasimhan
Materialized view Build Methods
BUILD IMMEDIATE BUILD DEFERRED
Adding the definition of the
materialized view into the schema
object in the data dictionary.
By Default, create Materialized view +
stores the result of the statement.
create Materialized view,
don’t store result of the
materialized view Query
execute dbms_mview.refresh('mv2',method=>'c');
For getting materialized view
result there is two ways
Alter materialized view as
refresh complete + enable
query rewrite + refresh
Alter materialized view mv3 refresh complete enable query rewrite;
execute dbms_mview.refresh('mv3');
Dr. Girija Narasimhan
For creating materialized view, there is two build methods are used in the
materialized view.
One is BUILD IMMEDIATE and another one is BUILD DEFERRED.
The BUILD IMMEDIATE method, it creates the materialized view and adding
the definition of the materialized view into the schema object in the data
dictionary.
And also search the base table according to the query given in the
materialized view (i.e SELECT statement) and stores their result in the
materialized view.
Dr. Girija Narasimhan
Dr. Girija Narasimhan
The build method clause is BUILD DEFERRED, it just creates the materialized view
but it don’ search and store the result in the materialized view.
This method disables the query rewrite, so first time materialized view is
executed by refresh complete method “C” using DBMS_MVIEW_REFRESH
statement and then it automatically it will enable the materialized view by
specifying the ENABLE QUERY REWRITE clause.

More Related Content

What's hot

12753028 scot-configuration-troubleshooting
12753028 scot-configuration-troubleshooting12753028 scot-configuration-troubleshooting
12753028 scot-configuration-troubleshootingkratos1979
 
Oracle Retro pay by element
Oracle Retro pay by elementOracle Retro pay by element
Oracle Retro pay by elementrunjithrocking
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questionsPyadav010186
 
UserParameter vs Zabbix Sender - 1º ZABBIX MEETUP DO INTERIOR-SP
UserParameter vs Zabbix Sender - 1º ZABBIX MEETUP DO INTERIOR-SPUserParameter vs Zabbix Sender - 1º ZABBIX MEETUP DO INTERIOR-SP
UserParameter vs Zabbix Sender - 1º ZABBIX MEETUP DO INTERIOR-SPAndré Déo
 
IntegrationBroker
IntegrationBrokerIntegrationBroker
IntegrationBrokermeghamystic
 
How to create payslip through self service
How to create payslip through self serviceHow to create payslip through self service
How to create payslip through self serviceFeras Ahmad
 
Base datos
Base datosBase datos
Base datosRchd
 
Ebook oracle-thuc-hanh-nguyen-huu-trong
Ebook oracle-thuc-hanh-nguyen-huu-trongEbook oracle-thuc-hanh-nguyen-huu-trong
Ebook oracle-thuc-hanh-nguyen-huu-trongngobacuong
 
List Processing in ABAP
List Processing in ABAPList Processing in ABAP
List Processing in ABAPsapdocs. info
 
Step By Step Install Oracle 10g Rac Asm On Windows
Step By Step Install Oracle 10g Rac Asm On WindowsStep By Step Install Oracle 10g Rac Asm On Windows
Step By Step Install Oracle 10g Rac Asm On Windowsjstorm
 
A step by-step guide on i doc-ale between two sap servers
A step by-step guide on i doc-ale between two sap serversA step by-step guide on i doc-ale between two sap servers
A step by-step guide on i doc-ale between two sap serverskrishna RK
 
Phantom bills of material
Phantom bills of materialPhantom bills of material
Phantom bills of materialLarry Sherrod
 
Student management system
Student management systemStudent management system
Student management systemStudent
 
Oracle EBS HRMS SETUP
Oracle EBS HRMS SETUPOracle EBS HRMS SETUP
Oracle EBS HRMS SETUPHussain Abbas
 
Conexión remota a base de datos con Oracle y MySQL
Conexión remota a base de datos con Oracle y MySQLConexión remota a base de datos con Oracle y MySQL
Conexión remota a base de datos con Oracle y MySQLIvan Luis Jimenez
 
Run report from menu Personalization كيفية تشغيل تقرير أو ما شابة من خلال شا...
Run report from menu  Personalization كيفية تشغيل تقرير أو ما شابة من خلال شا...Run report from menu  Personalization كيفية تشغيل تقرير أو ما شابة من خلال شا...
Run report from menu Personalization كيفية تشغيل تقرير أو ما شابة من خلال شا...Ahmed Elshayeb
 
Oracle EBS R12 Self service user manual
Oracle EBS R12 Self service user manualOracle EBS R12 Self service user manual
Oracle EBS R12 Self service user manualFeras Ahmad
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Thuan Nguyen
 

What's hot (20)

07.Advanced Abap
07.Advanced Abap07.Advanced Abap
07.Advanced Abap
 
12753028 scot-configuration-troubleshooting
12753028 scot-configuration-troubleshooting12753028 scot-configuration-troubleshooting
12753028 scot-configuration-troubleshooting
 
Oracle Retro pay by element
Oracle Retro pay by elementOracle Retro pay by element
Oracle Retro pay by element
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
 
UserParameter vs Zabbix Sender - 1º ZABBIX MEETUP DO INTERIOR-SP
UserParameter vs Zabbix Sender - 1º ZABBIX MEETUP DO INTERIOR-SPUserParameter vs Zabbix Sender - 1º ZABBIX MEETUP DO INTERIOR-SP
UserParameter vs Zabbix Sender - 1º ZABBIX MEETUP DO INTERIOR-SP
 
Dump Answers
Dump AnswersDump Answers
Dump Answers
 
IntegrationBroker
IntegrationBrokerIntegrationBroker
IntegrationBroker
 
How to create payslip through self service
How to create payslip through self serviceHow to create payslip through self service
How to create payslip through self service
 
Base datos
Base datosBase datos
Base datos
 
Ebook oracle-thuc-hanh-nguyen-huu-trong
Ebook oracle-thuc-hanh-nguyen-huu-trongEbook oracle-thuc-hanh-nguyen-huu-trong
Ebook oracle-thuc-hanh-nguyen-huu-trong
 
List Processing in ABAP
List Processing in ABAPList Processing in ABAP
List Processing in ABAP
 
Step By Step Install Oracle 10g Rac Asm On Windows
Step By Step Install Oracle 10g Rac Asm On WindowsStep By Step Install Oracle 10g Rac Asm On Windows
Step By Step Install Oracle 10g Rac Asm On Windows
 
A step by-step guide on i doc-ale between two sap servers
A step by-step guide on i doc-ale between two sap serversA step by-step guide on i doc-ale between two sap servers
A step by-step guide on i doc-ale between two sap servers
 
Phantom bills of material
Phantom bills of materialPhantom bills of material
Phantom bills of material
 
Student management system
Student management systemStudent management system
Student management system
 
Oracle EBS HRMS SETUP
Oracle EBS HRMS SETUPOracle EBS HRMS SETUP
Oracle EBS HRMS SETUP
 
Conexión remota a base de datos con Oracle y MySQL
Conexión remota a base de datos con Oracle y MySQLConexión remota a base de datos con Oracle y MySQL
Conexión remota a base de datos con Oracle y MySQL
 
Run report from menu Personalization كيفية تشغيل تقرير أو ما شابة من خلال شا...
Run report from menu  Personalization كيفية تشغيل تقرير أو ما شابة من خلال شا...Run report from menu  Personalization كيفية تشغيل تقرير أو ما شابة من خلال شا...
Run report from menu Personalization كيفية تشغيل تقرير أو ما شابة من خلال شا...
 
Oracle EBS R12 Self service user manual
Oracle EBS R12 Self service user manualOracle EBS R12 Self service user manual
Oracle EBS R12 Self service user manual
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 

Similar to OER UNIT 2-- MATERIALIZED VIEW- DATA WAREHOUSING

PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data TablesPostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data TablesSperasoft
 
Advance Sql Server Store procedure Presentation
Advance Sql Server Store procedure PresentationAdvance Sql Server Store procedure Presentation
Advance Sql Server Store procedure PresentationAmin Uddin
 
OER Unit 3 -Materialized view exercise solution
OER Unit 3 -Materialized view exercise solutionOER Unit 3 -Materialized view exercise solution
OER Unit 3 -Materialized view exercise solutionGirija Muscut
 
Scrollable Updatable
Scrollable UpdatableScrollable Updatable
Scrollable Updatableleminhvuong
 
Scrollable Updatable
Scrollable UpdatableScrollable Updatable
Scrollable Updatablephanleson
 
ZTE BSS Operation Quick Guide_Rev4.pptx
ZTE BSS Operation Quick Guide_Rev4.pptxZTE BSS Operation Quick Guide_Rev4.pptx
ZTE BSS Operation Quick Guide_Rev4.pptxVivi Gusti Anggraini
 
R12 d49656 gc10-apps dba 15
R12 d49656 gc10-apps dba 15R12 d49656 gc10-apps dba 15
R12 d49656 gc10-apps dba 15zeesniper
 
Docslide.us dynamic lookup-cache
Docslide.us dynamic lookup-cacheDocslide.us dynamic lookup-cache
Docslide.us dynamic lookup-cacheRaffaella D'angelo
 
How to customize Spring Boot?
How to customize Spring Boot?How to customize Spring Boot?
How to customize Spring Boot?GilWon Oh
 
Question IYou are going to use the semaphores for process sy.docx
Question IYou are going to use the semaphores for process sy.docxQuestion IYou are going to use the semaphores for process sy.docx
Question IYou are going to use the semaphores for process sy.docxaudeleypearl
 
Introduction to-mongo db-execution-plan-optimizer-final
Introduction to-mongo db-execution-plan-optimizer-finalIntroduction to-mongo db-execution-plan-optimizer-final
Introduction to-mongo db-execution-plan-optimizer-finalM Malai
 
Introduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerIntroduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerMydbops
 
Dynamics ax 2012 workflow development
Dynamics ax 2012 workflow development Dynamics ax 2012 workflow development
Dynamics ax 2012 workflow development Ahmed Farag
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedureftz 420
 
A Practical Approach to Building a Streaming Processing Pipeline for an Onlin...
A Practical Approach to Building a Streaming Processing Pipeline for an Onlin...A Practical Approach to Building a Streaming Processing Pipeline for an Onlin...
A Practical Approach to Building a Streaming Processing Pipeline for an Onlin...Databricks
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql ProcedurePooja Dixit
 

Similar to OER UNIT 2-- MATERIALIZED VIEW- DATA WAREHOUSING (20)

PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data TablesPostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
 
Les 07 Rman Rec
Les 07 Rman RecLes 07 Rman Rec
Les 07 Rman Rec
 
Advance Sql Server Store procedure Presentation
Advance Sql Server Store procedure PresentationAdvance Sql Server Store procedure Presentation
Advance Sql Server Store procedure Presentation
 
OER Unit 3 -Materialized view exercise solution
OER Unit 3 -Materialized view exercise solutionOER Unit 3 -Materialized view exercise solution
OER Unit 3 -Materialized view exercise solution
 
Scrollable Updatable
Scrollable UpdatableScrollable Updatable
Scrollable Updatable
 
Scrollable Updatable
Scrollable UpdatableScrollable Updatable
Scrollable Updatable
 
ZTE BSS Operation Quick Guide_Rev4.pptx
ZTE BSS Operation Quick Guide_Rev4.pptxZTE BSS Operation Quick Guide_Rev4.pptx
ZTE BSS Operation Quick Guide_Rev4.pptx
 
R12 d49656 gc10-apps dba 15
R12 d49656 gc10-apps dba 15R12 d49656 gc10-apps dba 15
R12 d49656 gc10-apps dba 15
 
Docslide.us dynamic lookup-cache
Docslide.us dynamic lookup-cacheDocslide.us dynamic lookup-cache
Docslide.us dynamic lookup-cache
 
Msql
Msql Msql
Msql
 
Cdc
CdcCdc
Cdc
 
How to customize Spring Boot?
How to customize Spring Boot?How to customize Spring Boot?
How to customize Spring Boot?
 
Question IYou are going to use the semaphores for process sy.docx
Question IYou are going to use the semaphores for process sy.docxQuestion IYou are going to use the semaphores for process sy.docx
Question IYou are going to use the semaphores for process sy.docx
 
MSSQL Queries.pdf
MSSQL Queries.pdfMSSQL Queries.pdf
MSSQL Queries.pdf
 
Introduction to-mongo db-execution-plan-optimizer-final
Introduction to-mongo db-execution-plan-optimizer-finalIntroduction to-mongo db-execution-plan-optimizer-final
Introduction to-mongo db-execution-plan-optimizer-final
 
Introduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerIntroduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizer
 
Dynamics ax 2012 workflow development
Dynamics ax 2012 workflow development Dynamics ax 2012 workflow development
Dynamics ax 2012 workflow development
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
 
A Practical Approach to Building a Streaming Processing Pipeline for an Onlin...
A Practical Approach to Building a Streaming Processing Pipeline for an Onlin...A Practical Approach to Building a Streaming Processing Pipeline for an Onlin...
A Practical Approach to Building a Streaming Processing Pipeline for an Onlin...
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 

More from Girija Muscut

Visualization using Tableau
Visualization using TableauVisualization using Tableau
Visualization using TableauGirija Muscut
 
Effective Visualization with Tableau
Effective Visualization with TableauEffective Visualization with Tableau
Effective Visualization with TableauGirija Muscut
 
Guruvayoor song with audio-Udayasthamana puja
Guruvayoor song with audio-Udayasthamana puja Guruvayoor song with audio-Udayasthamana puja
Guruvayoor song with audio-Udayasthamana puja Girija Muscut
 
Lakshmi lalli with audio
Lakshmi lalli with audioLakshmi lalli with audio
Lakshmi lalli with audioGirija Muscut
 
Bagyada laskhmi purandara dasa
Bagyada laskhmi purandara dasaBagyada laskhmi purandara dasa
Bagyada laskhmi purandara dasaGirija Muscut
 
Amba nee irangaayenil - papanasam sivan song
Amba nee irangaayenil - papanasam sivan songAmba nee irangaayenil - papanasam sivan song
Amba nee irangaayenil - papanasam sivan songGirija Muscut
 
Mahalakshmi jagan madha - papanasm sivan tamil song
Mahalakshmi jagan madha  - papanasm sivan tamil songMahalakshmi jagan madha  - papanasm sivan tamil song
Mahalakshmi jagan madha - papanasm sivan tamil songGirija Muscut
 
Sowbhagayaha laskhmi varuvai nee tamil song
Sowbhagayaha laskhmi varuvai nee tamil songSowbhagayaha laskhmi varuvai nee tamil song
Sowbhagayaha laskhmi varuvai nee tamil songGirija Muscut
 
Bega baro Bega baro Neela Megha Varna-Vadhiraja Theertha
Bega baro Bega baro Neela Megha Varna-Vadhiraja TheerthaBega baro Bega baro Neela Megha Varna-Vadhiraja Theertha
Bega baro Bega baro Neela Megha Varna-Vadhiraja TheerthaGirija Muscut
 
Saraswathi bhajan 1 with tamil meaning
Saraswathi bhajan 1 with tamil meaningSaraswathi bhajan 1 with tamil meaning
Saraswathi bhajan 1 with tamil meaningGirija Muscut
 
Aneyu karadare -Purandara Dasar.
Aneyu karadare -Purandara Dasar.Aneyu karadare -Purandara Dasar.
Aneyu karadare -Purandara Dasar.Girija Muscut
 
Maithriam Bhajatha with tamil meaning (lyrics)
Maithriam Bhajatha with tamil meaning (lyrics)Maithriam Bhajatha with tamil meaning (lyrics)
Maithriam Bhajatha with tamil meaning (lyrics)Girija Muscut
 
Unit 4 scd2-exercise 1-solution
Unit 4 scd2-exercise 1-solutionUnit 4 scd2-exercise 1-solution
Unit 4 scd2-exercise 1-solutionGirija Muscut
 
Unit 2 - Slowly Changing Dimension Type 1 (SCD1) (insert)
Unit 2  - Slowly Changing Dimension Type 1 (SCD1) (insert)Unit 2  - Slowly Changing Dimension Type 1 (SCD1) (insert)
Unit 2 - Slowly Changing Dimension Type 1 (SCD1) (insert)Girija Muscut
 
Slowly Changing Dimension Type 1 (SCD 1) exercise 2 solution insert and update
Slowly Changing Dimension Type 1 (SCD 1) exercise 2 solution insert and updateSlowly Changing Dimension Type 1 (SCD 1) exercise 2 solution insert and update
Slowly Changing Dimension Type 1 (SCD 1) exercise 2 solution insert and updateGirija Muscut
 

More from Girija Muscut (20)

Tamil Nalvar
Tamil Nalvar Tamil Nalvar
Tamil Nalvar
 
Visualization using Tableau
Visualization using TableauVisualization using Tableau
Visualization using Tableau
 
Introduction to ml
Introduction to mlIntroduction to ml
Introduction to ml
 
Effective Visualization with Tableau
Effective Visualization with TableauEffective Visualization with Tableau
Effective Visualization with Tableau
 
Guruvayoor song with audio-Udayasthamana puja
Guruvayoor song with audio-Udayasthamana puja Guruvayoor song with audio-Udayasthamana puja
Guruvayoor song with audio-Udayasthamana puja
 
Lakshmi lalli with audio
Lakshmi lalli with audioLakshmi lalli with audio
Lakshmi lalli with audio
 
Bagyada laskhmi purandara dasa
Bagyada laskhmi purandara dasaBagyada laskhmi purandara dasa
Bagyada laskhmi purandara dasa
 
Lakshmi lalli
Lakshmi lalliLakshmi lalli
Lakshmi lalli
 
Amba nee irangaayenil - papanasam sivan song
Amba nee irangaayenil - papanasam sivan songAmba nee irangaayenil - papanasam sivan song
Amba nee irangaayenil - papanasam sivan song
 
Mahalakshmi jagan madha - papanasm sivan tamil song
Mahalakshmi jagan madha  - papanasm sivan tamil songMahalakshmi jagan madha  - papanasm sivan tamil song
Mahalakshmi jagan madha - papanasm sivan tamil song
 
Sowbhagayaha laskhmi varuvai nee tamil song
Sowbhagayaha laskhmi varuvai nee tamil songSowbhagayaha laskhmi varuvai nee tamil song
Sowbhagayaha laskhmi varuvai nee tamil song
 
Bega baro Bega baro Neela Megha Varna-Vadhiraja Theertha
Bega baro Bega baro Neela Megha Varna-Vadhiraja TheerthaBega baro Bega baro Neela Megha Varna-Vadhiraja Theertha
Bega baro Bega baro Neela Megha Varna-Vadhiraja Theertha
 
Rama Nama Bhajan
Rama Nama BhajanRama Nama Bhajan
Rama Nama Bhajan
 
Saratha devi song 1
Saratha devi song 1Saratha devi song 1
Saratha devi song 1
 
Saraswathi bhajan 1 with tamil meaning
Saraswathi bhajan 1 with tamil meaningSaraswathi bhajan 1 with tamil meaning
Saraswathi bhajan 1 with tamil meaning
 
Aneyu karadare -Purandara Dasar.
Aneyu karadare -Purandara Dasar.Aneyu karadare -Purandara Dasar.
Aneyu karadare -Purandara Dasar.
 
Maithriam Bhajatha with tamil meaning (lyrics)
Maithriam Bhajatha with tamil meaning (lyrics)Maithriam Bhajatha with tamil meaning (lyrics)
Maithriam Bhajatha with tamil meaning (lyrics)
 
Unit 4 scd2-exercise 1-solution
Unit 4 scd2-exercise 1-solutionUnit 4 scd2-exercise 1-solution
Unit 4 scd2-exercise 1-solution
 
Unit 2 - Slowly Changing Dimension Type 1 (SCD1) (insert)
Unit 2  - Slowly Changing Dimension Type 1 (SCD1) (insert)Unit 2  - Slowly Changing Dimension Type 1 (SCD1) (insert)
Unit 2 - Slowly Changing Dimension Type 1 (SCD1) (insert)
 
Slowly Changing Dimension Type 1 (SCD 1) exercise 2 solution insert and update
Slowly Changing Dimension Type 1 (SCD 1) exercise 2 solution insert and updateSlowly Changing Dimension Type 1 (SCD 1) exercise 2 solution insert and update
Slowly Changing Dimension Type 1 (SCD 1) exercise 2 solution insert and update
 

Recently uploaded

COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...naitiksharma1124
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Andrea Goulet
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIInflectra
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdftimtebeek1
 
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...OnePlan Solutions
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Conceptsthomashtkim
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypseTomasz Kowalczewski
 
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptxFrom Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptxNeo4j
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)Roberto Bettazzoni
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfkalichargn70th171
 
architecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfarchitecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfWSO2
 
Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024Chirag Panchal
 
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with GraphGraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with GraphNeo4j
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Clinic
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Henry Schreiner
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AIAGATSoftware
 
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
Workshop -  Architecting Innovative Graph Applications- GraphSummit MilanWorkshop -  Architecting Innovative Graph Applications- GraphSummit Milan
Workshop - Architecting Innovative Graph Applications- GraphSummit MilanNeo4j
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMarkus Moeller
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...drm1699
 

Recently uploaded (20)

COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST API
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdf
 
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Concepts
 
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptxFrom Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
 
architecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfarchitecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdf
 
Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024
 
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with GraphGraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
Workshop -  Architecting Innovative Graph Applications- GraphSummit MilanWorkshop -  Architecting Innovative Graph Applications- GraphSummit Milan
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
 

OER UNIT 2-- MATERIALIZED VIEW- DATA WAREHOUSING

  • 1. Dr. Girija Narasimhan PART 1 View Vs. Materialized view OER CHAPTER 2
  • 2. Using materialized views, it is possible to pre calculate complex joins and execute the aggregate operation i.e summary data and also stores the result set of the table in the database. In future the end users may query the table and views the detail information. The query rewrite mechanism in the Oracle server automatically rewrites the SQL query to use the summary tables. This technique improves the query execution time and performance. Dr. Girija Narasimhan
  • 3. Dr. Girija Narasimhan create table item(itemno number(3) primary key ,iname varchar2(10),price number(4)); insert into item values(1,'milk',20); insert into item values(2,'Bread',30); insert into item values(3,'Juice',40); Table Materialized View select itemno,Iname from item ; create materialized view mv as select itemno,iname from item; select * from MV ; ITEMNO INAME ------ ----- 1 milk 2 Bread 3 Juice ITEMNO INAME ------- ----- 1 milk 2 Bread 3 Juice
  • 4. Dr. Girija Narasimhan •A materialized view is a database object. • It is physically stored in the database. •Because materialized view tables are having local copies of data suppose base table is located remotely. •It is also useful to create summary table based on aggregation of the base table data. •The materialized view stores both query and the result of a query.
  • 5. Dr. Girija Narasimhan A view is a logical entity or virtual table. The view is attached with SQL statement or query, which will store in the database in the system table space. It is used as similar like database table or base table (i.e item table). Whenever query is fired against the database table or base table (i.e. item table), The view “v” execute the stored SQL statement ( i.e. select * from item) and creates a temporary table in the memory. It do not stores the query result, it only execute and return the result /output. It means it will store only query not result.
  • 6. Dr. Girija Narasimhan Both base table and view table are identical, since both the base table and view table “Rowid’s” are same. Rowid is the physical address for each record in the database and it is a fixed- length binary data. So, it returns the same value and also any one table is updated the other table automatically returns the exact same result. Base table and view table Rowid’s are same
  • 7. Dr. Girija Narasimhan Whenever update the base table (item table), the view also automatically update the change. Otherwise update the view (“v”), the base table (item table) also automatically modifies the changes. For example after the update, the view data matches the table data but the materialized view data does not.
  • 8. Dr. Girija Narasimhan The reason for materialized view is not reflecting the updation of base table like view is, materialized view storing the copy of data in a separate physically place in the database. The given below table clearly shows base table rowid and materialized view rowid are not same.
  • 9. Dr. Girija Narasimhan The reason for materialized view is not reflecting the updation of base table is, materialized view storing the copy of data in a separate physically place in the database. i.e. both have different Rowid Rowid is the physical address for each record in the database and it is a fixed-length binary data Table Materialized View update item set iname = upper(iname); select itemno,iname from item ; select * from MV ; ITEMNO INAME ---------- ---------- 1 MILK 2 BREAD 3 JUICE ITEMNO INAME ------- ----- 1 milk 2 Bread 3 Juice
  • 10. Dr. Girija Narasimhan To apply refresh technique to materialized view table for synchronize With base table data. Refresh complete, Refresh Fast, Refresh Force
  • 11. Dr. Girija Narasimhan Mode of Refresh Technique PART 2
  • 12. Dr. Girija Narasimhan ON DEMAND ON COMMIT Mode of Refresh By default mode, it need manual refresh execution procedure Automatic refresh, no need of execute refresh procedure
  • 13. Dr. Girija Narasimhan There is two mode of refresh technique is available, one is “ON DEMAND” and other one is “ON COMMIT”. create materialized view mv as select itemno,iname from item; = create materialized view mv REFRESH ON DEMAND as select itemno,iname from item; Table Materialized View update item set iname = upper(iname); select itemno,iname from item ; select * from MV ; ITEMNO INAME ---------- ---------- 1 MILK 2 BREAD 3 JUICE ITEMNO INAME ------- ----- 1 milk 2 Bread 3 Juice
  • 14. Dr. Girija Narasimhan For matching updating of data in materialized view table (mv) and base table (item), the refresh procedures are available in the DBMS_MVIEW package. When execute the refresh procedure, the materialized view table (mv) synchronized with base table (item) Table Materialized View select * from item; select itemno,iname from MV ; ITEMNO INAME ------ ---------- 1 MILK 2 BREAD 3 JUICE ITEMNO INAME ------ -------- 1 MILK 2 BREAD 3 JUICE SQL> execute dbms_mview.refresh( 'MV' ); PL/SQL procedure successfully completed.
  • 15. Dr. Girija Narasimhan The other method is called “ON COMMIT”, Unlike manual refresh method, it doesn’t need “DBMS_MVIEW.REFRESH” to execute. Create materialized view mv on commit as select * from item; Select itemno,iname from mv ITEMNO INAME --------- --------- 1 Fruits 2 COFFEE 8 Biscutts 5 rice
  • 16. Dr. Girija Narasimhan select * from item; ITEMNO INAME ---------- ---------- 3 Water 1 Fruits 2 COFFEE 8 Biscutts 5 rice Select itemno,iname from mv; ITEMNO INAME ------- ----------- 1 Fruits 2 COFFEE 8 Biscutts 5 rice 3 Water Insert into item values(3,’Water’) Commit; It automatically refreshed, i.e it is synchronized with base table data just by “COMMIT” statement.
  • 18. Dr. Girija Narasimhan Create Materialized view using Refresh options Refresh complete Refresh Fast Refresh Force Never Refresh Method =‘C’ execute Method =‘F’ It create new result set, with new row id No Need to mention method “F” in the Execute statement. It don’t create new result set in different row id, same existing row id User specifying saying Method either ‘C’ or ‘F’ Method =>’C’ Method =>’F’ Method =>’ ?’ System decides based on situation it will automatically assign either ‘C’ or ‘F’ ALTER MV into refresh complete, refresh fast, refresh force Execute statement must be method =>’C’ otherwise error occur
  • 19. Dr. Girija Narasimhan Refresh Complete PART 5
  • 20. Dr. Girija Narasimhan When a refresh complete occurs in the materialized view's defining query is executed and the complete result set replaces the data currently existed in the materialized view. As a result, it completely re-creates the result set of the materialized view Before Execute After Execute using “C” method select rowid,itemno,iname from mv2; execute DBMS_MVIEW.REFRESH(LIST => 'MV2', method=>'c'); select rowid,itemno,iname from mv2; ROWID ITEMNO INAME AAAMEQAAEAAAAB0AAA 1 MILK AAAMEQAAEAAAAB0AAB 2 BREAD AAAMEQAAEAAAAB0AAC 3 JUICE ROWID ITEMNO INAME- AAAMEQAAEAAAAB0AAD 1 MILK AAAMEQAAEAAAAB0AAE 2 BREAD AAAMEQAAEAAAAB0AAF 3 JUICE SQL> create materialized view mv2 refresh complete as select * from item; The rowids after execute differs from the before execute, even though the data in base table ITEM was unchanged
  • 21. Dr. Girija Narasimhan execute DBMS_MVIEW.REFRESH(LIST => 'MV2', METHOD => 'C' ); The "list" parameter accepts a list of materialized views to refresh (i.e “mv2”) and the "method" parameter accepts a "C", for Complete refresh. (i.e ‘C’ means Complete refresh) The limitation of refresh complete is , suppose if a materialized view have many rows and the base tables values are changed infrequently then refresh complete is time-consuming method or over slow. Another issue is due to bad connections sometimes the refresh completes never finishing their process.
  • 22. Dr. Girija Narasimhan execute DBMS_MVIEW.REFRESH(LIST => 'MV2', METHOD =>'F' ); create materialized view mv2 refresh complete as select * from item; Before Execute After Execute using “F” method select rowid,itemno,iname from mv2; execute DBMS_MVIEW.REFRESH(LIST => 'MV2', method=>'F'); select rowid,itemno,iname from mv2; ROWID ITEMNO INAME AAAMEQAAEAAAAB0AAA 1 MILK AAAMEQAAEAAAAB0AAB 2 BREAD AAAMEQAAEAAAAB0AAC 3 JUICE ROWID ITEMNO INAME AAAMEQAAEAAAAB0AAA 1 MILK AAAMEQAAEAAAAB0AAB 2 BREAD AAAMEQAAEAAAAB0AAC 3 JUICE Even use “Refresh complete” in the materialized view creation, even though call the method “F” it will do “refresh fast”. The notable point is “Rowid” before and after executing refresh is not changed.
  • 23. Dr. Girija Narasimhan PART 4 Refresh Fast
  • 24. Dr. Girija Narasimhan REFRESH FAST clause used in the CREATE MATERIALIZED VIEW command tells the oracle suppose no need to mention refresh option in the execution. The above statement is not mentioning any “method =>’F’, but create materialized view “mv” is using refresh fast. So, the default refresh method in the execute statement refresh fast only create materialized view MV refresh fast as select * from item; Execute dbms_mview.refresh( 'MV' );
  • 25. Dr. Girija Narasimhan create materialized view mv refresh fast as select * from item; Before Execute After Execute using “F” method select rowid,itemno,iname from mv; execute DBMS_MVIEW.REFRESH(LIST => 'MV', method=>'F'); select rowid,itemno,iname from mv; ROWID ITEMNO INAME AAAMEQAAEAAAAB0AAA 1 MILK AAAMEQAAEAAAAB0AAB 2 BREAD AAAMEQAAEAAAAB0AAC 3 JUICE ROWID ITEMNO INAME AAAMEQAAEAAAAB0AAA 1 MILK AAAMEQAAEAAAAB0AAB 2 BREAD AAAMEQAAEAAAAB0AAC 3 JUICE update item set iname='JAM' where itemno=3 execute DBMS_MVIEW.REFRESH('MV’'); select rowid,itemno,iname from mv; ROWID ITEMNO INAME ----------------------------------------------------------------------------------------------------- AAAMEQAAEAAAAB0AAA 1 MILK AAAMEQAAEAAAAB0AAB 2 BREAD AAAMEQAAEAAAAB0AAC 3 JAM The benefit of using REFRESH FAST is, it don’t create entire new result set using new rowid like REFRESH COMPLETE. The value was updated in the materialized view without changing the “Rowid”.
  • 26. Dr. Girija Narasimhan PART 5 Refresh Force
  • 27. Dr. Girija Narasimhan “REFRESH FORCE” clause to decide which performance is most appropriate for query rewrite using DML operation either refresh fast or refresh complete based on the Operation. Create materialized view mv3 refresh force as select max(itemno) as max from item; SQL> select rowid,max from mv3; ROWID MAX ----------------------------------------------------------------------------------- AAAMFEAAEAAAACMAAA 6 update item set iname='jam' where itemno=6; execute dbms_mview.refresh('mv3'); SQL> select rowid,max from mv3; ROWID MAX --------------------------------------- ---------- AAAMFEAAEAAAACMAAB 6 the update operation in the materialized and after execute it changes the rowid; here refresh force performs the refresh complete method.
  • 28. Dr. Girija Narasimhan Create materialized view mv3 refresh force as select itemno,iname from item where itemno>4; SQL> select rowid,itemno,iname from mv3; ROWID ITEMNO INAME ----------------------------------- --------- ---------- AAAMFGAAEAAAACMAAA 5 water AAAMFGAAEAAAACMAAB 6 jam update item set iname='pepsi' where itemno=5; execute dbms_mview.refresh('mv3'); SQL> select rowid,itemno,iname from mv3; ROWID ITEMNO INAME ------------------------------------------------------------ ------------------------------ AAAMFGAAEAAAACMAAA 5 pepsi AAAMFGAAEAAAACMAAB 6 jam update operation are executed, here Rowid is not changing. The refresh force is performing the “Refresh fast”
  • 29. Dr. Girija Narasimhan PART 6 Never Refresh using ALTER Refresh complete
  • 30. Dr. Girija Narasimhan Oracle Database will ignore any REFRESH statement on the materialized view and prevents any refresh method such as FAST or COMPLETE by using NEVER REFRESH in create materialized view statement. Create materialized view mv3 never refresh as select itemno,iname from item where itemno>4; SQL> select rowid,itemno,iname from mv3; ROWID ITEMNO INAME -------------------------------------- ---------- ---------- AAAMFJAAEAAAACMAAA 5 pepsi AAAMFJAAEAAAACMAAB 6 jam SQL> update item set iname='water' where itemno=5; SQL> execute dbms_mview.refresh('mv3'); BEGIN dbms_mview.refresh('mv3'); END; *ERROR at line 1:ORA-23538: cannot explicitly refresh a NEVER REFRESH materialized view ("MV3") ORA-06512: at "SYS.DBMS_SNAPSHOT", line 1883 ORA-06512: at "SYS.DBMS_SNAPSHOT", line 2089 ORA-06512: at "SYS.DBMS_SNAPSHOT", line 2058
  • 31. Dr. Girija Narasimhan Alter the materialized view into refresh complete and then execute the materialized view. Now, I don’t give error message. alter materialized view mv3 refresh complete; execute dbms_mview.refresh('mv3'); SQL> select rowid,itemno,iname from mv3; ROWID ITEMNO INAME ------------------------------------------------------------------------------------------- AAAMFJAAEAAAACMAAC 5 water AAAMFJAAEAAAACMAAD 6 jam
  • 32. Dr. Girija Narasimhan PART 7 Never Refresh using ALTER Refresh Fast
  • 33. Dr. Girija Narasimhan Instead of altering materialized view into refresh complete, alter into refresh fast. Then try any DML operations, it gives the error. The reason is, it alters the materialized view into “read-only" type. So, it doesn’t allow any DML operation. In this case, call “refresh complete” for execution. Complete refresh creates the entirely new set.
  • 34. Dr. Girija Narasimhan alter materialized view mv3 refresh fast; execute dbms_mview.refresh('mv3'); Insert into item values(8,’Biscutts’) BEGIN dbms_mview.refresh('mv3'); END;*ERROR at line 1: ORA-12057: materialized view "SCOTT"."MV3" is INVALID and must complete refresh ORA-06512: at "SYS.DBMS_SNAPSHOT", line 1883 ORA-06512: at "SYS.DBMS_SNAPSHOT", line 2089 ORA-06512: at "SYS.DBMS_SNAPSHOT", line 2058 ORA-06512: at line 1 execute dbms_mview.refresh( list => 'MV3',method=>'c'); SQL> select rowid,itemno,iname from mv3; ROWID ITEMNO INAME ------------------ ---------- ---------- AAAMFJAAEAAAACMAAE 5 water AAAMFJAAEAAAACMAAF 6 jam AAAMFJAAEAAAACMAAG 8 Biscutts
  • 35. Dr. Girija Narasimhan PART 8 Never Refresh using ALTER Refresh Force
  • 36. Dr. Girija Narasimhan Create materialized view mv3 never refresh as select itemno,iname from item where itemno>4; alter materialized view mv3 refresh force; select rowid,itemno,iname from mv3; ROWID ITEMNO INAME ----------------------------------- ---------- ---------- AAAMFPAAEAAAACMAAA 5 Lemon AAAMFPAAEAAAACMAAB 8 Biscutts update item set iname='rice' where itemno=5; SQL> execute DBMS_MVIEW.REFRESH(LIST => 'MV3',method=>'?'); SQL> select rowid,itemno,iname from mv3; ROWID ITEMNO INAME ------------------------------------- ---------- ---------- AAAMFPAAEAAAACMAAC 5 rice AAAMFPAAEAAAACMAAD 8 Biscutts
  • 37. Dr. Girija Narasimhan Alter the materialized view from never fresh into refresh force, now execute the update operation. In the execute statement, the method “?” means refresh force. But refresh force applies only refresh complete method, not refresh fast. It shows in the result set that “rowid” has changed
  • 38. Dr. Girija Narasimhan PART 9 Create Materialized view Log MLOG$
  • 39. Dr. Girija Narasimhan As seen earlier topic, complete refresh is time-consuming and occupying storage space. These drawbacks are rectified by applying Materialized View Log The materialized view captures only the changed rows occur in the base table, it don’t captures the entire value. This technique is called Materialized View Log and “incremental" refreshing Usually, a Materialized View Log takes less time than a complete refresh.
  • 40. Dr. Girija Narasimhan DESC ITEM; Name Null? Type ----------------------------------------- -------- ---------------------------- ITEMNO NOT NULL NUMBER(3) INAME VARCHAR2(10) create materialized view log on item; In this materialized view log, the name for materialized view is not given name. For example in the previous materialized view used “mv”, “mv2" like that. Because a base table has only one materialized view log related at a time, so a separate materialized view name is not required.
  • 41. Dr. Girija Narasimhan describe MLOG$_item; Name Null? Type ----------------------------------------- -------- ---------------------------- ITEMNO NUMBER(3) SNAPTIME$$ DATE DMLTYPE$$ VARCHAR2(1) OLD_NEW$$ VARCHAR2(1) CHANGE_VECTOR$$ RAW(255) Once materialized view log was created, it automatically generates a log file called MLOG$_<base table> The MLOG$_item.itemno column copy the base table’s primary key column item.itemno. The MLOG$ also uses in the primary key column data type and size is as same as base table item i.e data type is NUMBER(3). The other MLOG$ columns are system generated for example SNAPTIME$$, DMLTYPE$$,OLDNEW$$,CHANGE_VECTOR$$.
  • 42. Dr. Girija Narasimhan PART 10 Materialized view Log DMLTYPE$$
  • 43. Dr. Girija Narasimhan UPDATE ITEM set INAME=‘COFFEE’ where ITEMNO = 2 ; INSERT into ITEM(ITEMNO,INAME)values (4,’Biscutts’); select itemno, dmltype$$ as dmltype from MLOG$_item; ITEMNO D -------------------- 2 U 4 I select * from MLOG$_ITEM; no rows selected Materialized view log is initially empty. Rows are automatically added to MLOG$_ITEM when base table ITEM is executed by any DML operation such that UPDATE, DELETE or INSERT. The result of the MLOG$_item, shows number of changes occur in the base table. Here “U” – update operation in itemno 2 and “I”- insert operation occurred in itemno 4 in the base table item.
  • 44. Dr. Girija Narasimhan rollback ; select itemno, dmltype$$ from MLOG$_item; no rows selected Suppose, if rollback all the DML operation executed in the base table, the MLOG$_ITEM is again empty. The changes are not committed in the base table item. The materialized view log is using “on commit” by default. That is a reason it don’t need execute any “DBMS_MVIEW.REFRESH” procedure to initiate the refresh in the materialized view.
  • 45. Dr. Girija Narasimhan PART 11 Materialized view Log WITH Clause summary
  • 46. Dr. Girija Narasimhan Materialized view Log With Primary Key Row id Column name Sequence$$ By default mlog$ have primary key column. It will take data type and size of the base table. It will create M_ROW$$ column in the Mlog$ Its data type and size is VARCHAR2(255). It will replace the primary key column in the Mlog$ Suppose newly creating mlog$ with primary key,rowid then mlog$ have both primary key and rowid. Already created mlog$ use add rowid The WITH clause can also contain a list of specific base table columns. it will use data type and size of column is as mentioned in the base table Using “WITH SEQUENCE” in the materialized view log statement includes SEQUENCE$$ column in MLOG$ table. This SEQUENCE$$ column is help oracle to keep storing exact ordering information of mixed combination of DML operations used in the multiple base table.
  • 47. Dr. Girija Narasimhan PART 12 Materialized view Log WITH Clause Primary Key
  • 48. Dr. Girija Narasimhan Any way by default, the MLOG$ includes the primary key of the base table. To include the base table's primary key column in a materialized view log the WITH PRIMARY KEY clause can be specified. create materialized view log on item WITH PRIMARY KEY = create materialized view log on item
  • 49. Dr. Girija Narasimhan DESC ITEM; Name Null? Type ----------------------------------------- -------- ---------------------------- ITEMNO NOT NULL NUMBER(3) INAME VARCHAR2(10) drop materialized view log on item ; create materialized view log on item WITH PRIMARY KEY ; desc mlog$_item; Name Null? Type --------------------------------------------------------------- ITEMNO NUMBER(3) SNAPTIME$$ DATE DMLTYPE$$ VARCHAR2(1) OLD_NEW$$ VARCHAR2(1) CHANGE_VECTOR$$ RAW(255)
  • 50. Dr. Girija Narasimhan PART 13 Materialized view Log WITH Clause ROWID
  • 51. Dr. Girija Narasimhan ROWID to be specified in the materialized view log using “with” clause drop materialized view log on item ;create materialized view log on item WITH ROWID ; desc mlog$_item; Name Null? Type ----------------------------------- --------- -------- ------- M_ROW$$ VARCHAR2(255) SNAPTIME$$ DATE DMLTYPE$$ VARCHAR2(1) OLD_NEW$$ VARCHAR2(1) CHANGE_VECTOR$$ RAW(255) Using “WITH” clause include the ROWID in the materialized log view, instead of primary key column ITEMNO it is substituted by M_ROW$$ column.
  • 52. Dr. Girija Narasimhan drop materialized view log on item; create materialized view log on item WITH ROWID, PRIMARY KEY; desc mlog$_item Name Null? Type ----------------------------- -------- ---------------------------- ITEMNO NUMBER(3) M_ROW$$ VARCHAR2(255) SNAPTIME$$ DATE DMLTYPE$$ VARCHAR2(1) OLD_NEW$$ VARCHAR2(1) CHANGE_VECTOR$$ RAW(255) It is also possible to include both rowid and primary key in MLOG$ table, by using WITH clause,
  • 53. Dr. Girija Narasimhan SQL> ALTER MATERIALIZED VIEW LOG ON item ADD ROWID; SQL> DESC MLOG$_ITEM; Name Null? Type -------------------------------------------------------- ITEMNO NUMBER(3) SNAPTIME$$ DATE DMLTYPE$$ VARCHAR2(1) OLD_NEW$$ VARCHAR2(1) CHANGE_VECTOR$$ RAW(255) M_ROW$$ VARCHAR2(255) Another alternative method for include rowid and primary in the MLOG$ table without dropping the existing MLOG$ table. In other words, say that there is no need to recreate MLOG$ table.
  • 54. Dr. Girija Narasimhan PART 14 Materialized view Log WITH Clause COLUMN NAME
  • 55. Dr. Girija Narasimhan The WITH clause can also contain a list of specific base table columns. For example include the Iname column from the base table. Here, it will use data type and size of column is as mentioned in the base table such as “VARCHAR2(10)”. create materialized view log on item WITH (iname); SQL> desc mlog$_item; Name Null? Type ------------------------------------ -------- -------------- ITEMNO NUMBER(3) INAME VARCHAR2(10) SNAPTIME$$ DATE DMLTYPE$$ VARCHAR2(1) OLD_NEW$$ VARCHAR2(1) CHANGE_VECTOR$$ RAW(255) 55
  • 56. Dr. Girija Narasimhan PART 15 Materialized view Log WITH Clause SEQUENCE$$
  • 57. Dr. Girija Narasimhan Using “WITH SEQUENCE” in the materialized view log statement includes SEQUENCE$$ column in MLOG$ table. create materialized view log on ITEM WITH SEQUENCE ; desc mlog$_item; Name Null? Type ------------------------------------------------------------------------------------- ITEMNO NUMBER(3) SEQUENCE$$ NUMBER SNAPTIME$$ DATE DMLTYPE$$ VARCHAR2(1) OLD_NEW$$ VARCHAR2(1) CHANGE_VECTOR$$ RAW(255) This SEQUENCE$$ column is help oracle to keep storing exact ordering information of mixed combination of DML operations used in the multiple base table. For example insert, update and delete operations are executed on multiple base tables in different order.
  • 58. Dr. Girija Narasimhan drop materialized view log on ITEM ; create materialized view log on ITEM WITH SEQUENCE ; create materialized view log on SUPPLIER WITH SEQUENCE ; INSERT into ITEM values ( 6, 'Chocolate' ); INSERT into supplier values (4,’Rasna’); delete from item where itemno=4; UPDATE supplier set sname = ‘AVT Tea’ where suppno=3 ; commit; select SEQUENCE$$, itemno, dmltype$$ from mlog$_item; select SEQUENCE$$, suppno, dmltype$$ from mlog$_supplier ; SEQUENCE$$ itemno DMLtype$$ ---------- ---------- ------------------------- 1 6 I 3 4 D SEQUENCE$$ SUPPNO dmltype$$ ---------- ---------- - ---------------------------- 2 4 I 4 3 U
  • 59. Dr. Girija Narasimhan PART 16 Materialized view Log OLD_NEW$$
  • 60. Dr. Girija Narasimhan The OLD_NEW$$ column in the MLOG$ stores the values before updating the base table value. It is helpful in future for verifying the previous value in the same column and also indicates the base table values were refreshed. UPDATE item set iname= 'water' where itemno = 3; select itemno, iname, old_new$$ from mlog$_item; ITEMNO INAME OLD_NEW$$ ---------- ---------- ---------- 3 JUICE O
  • 61. Dr. Girija Narasimhan PART 17 Materialized view Log INCLUDING NEW VALUES
  • 62. Dr. Girija Narasimhan By default the materialized view log exclude the new value. Specify EXCLUDING to disable the recording of new values in the log. In some situations, it helps to identify both the old value and the new value explicitly saved in the materialized view log. INCLUDING NEW VALUES tells the oracle database to save old and new values in OLD_NEW$$ column for update DML operations in the materialized view log create materialized view log on ITEM with sequence (INAME ) INCLUDING NEW VALUES ; update ITEM set INAME = 'Fruits' where ITEMNO = 1 ; select sequence$$, itemno, iname, old_new$$ from mlog$_item order by sequence$$; SEQUENCE$$ ITEMNO INAME OLD_NEW$$ ----------------------------------------------------------------------- 5 1 MILK O 6 1 Fruits N
  • 63. Dr. Girija Narasimhan There is no need to store the new value for an update because it can be derived by applying the change vector (a RAW value stored in CHANGE_VECTOR$$, which Oracle uses internally during refreshes) to the old value.
  • 64. Dr. Girija Narasimhan PART 18 COMMENTS ON Materialized view
  • 65. Dr. Girija Narasimhan It is possible to add comment in materialized view. This comment statement added into the data dictionary for the already existing materialized view table. The comment table has three columns in the data dictionary regarding the materialized view: Owner - owner of the materialized view Mview_name - materialized view name Comments -Comment on the materialized view SQL> comment on materialized view mv is 'ITEM Information'; Comment created. SELECT MVIEW_NAME, COMMENTS FROM USER_MVIEW_COMMENTS WHERE MVIEW_NAME = ‘MV'; MVIEW_NAME COMMENTS ------------------------------------------------ MV ITEM Information
  • 66. Dr. Girija Narasimhan There is three types of comment view are available in the data dictionary for materialized view. •USER_MVIEW_COMMENTS display the current user comments on the materialized views. This view does not display the OWNER column. It will display only Mview_name, comments column. •DBA_MVIEW_COMMENTS displays the database comments on the materialized views. It will display all the three columns such as owner, mview_name, comments. •ALL_MVIEW_COMMENTS displays all the current user comments on the materialized view. It will display all the three columns such as owner, mview_name, comments.
  • 67. Dr. Girija Narasimhan SELECT OWNER, MVIEW_NAME, COMMENTS FROM ALL_MVIEW_COMMENTS; OWNER MVIEW_NAME COMMENTS ---------------------------------------------------------- SCOTT MV1 Snapshot table for snapshot SCOTT.MV1 SCOTT MV ITEM Information SCOTT MV2 ITEM DESCRIPTION Drop comments on materialized view SQL>comment on materialized view mv is ' '; Syntax: Comment on materialized view <name of the materialized view> is ‘< within single code give blank space>‘;
  • 68. Dr. Girija Narasimhan PART 19 Materialized view Using “For Update”
  • 69. Dr. Girija Narasimhan By default materialized view tables are read-only. Suppose tried with DML operation in the materialized view table causes the error message SQL> INSERT into mv(ITEMNO,INAME)values (5,'Chocolate'); INSERT into mv(ITEMNO,INAME)values (5,'Chocolate') * ERROR at line 1: ORA-01732: data manipulation operation not legal on this view Suppose materialized view is created using FOR UPDATE clause, then it enables DML operations directly in the materialized view. This type of materialized view is called updatable materialized view.
  • 70. Dr. Girija Narasimhan SQL> drop materialized view mv; SQL> create materialized view mv for update as select itemno,iname from item; SQL> select itemno,iname from item; ITEMNO INAME ---------- ---------- 1 MILK 2 COFFEE 3 JUICE 4 Biscutts SQL> select itemno,iname from mv; ITEMNO INAME ---------- ---------- 1 MILK 2 COFFEE 3 JUICE 4 Biscutts The data in the materialized view table “mv” and base table “item” are identical. Now insert one record only in materialized view not in the base table “item” SQL> INSERT into mv(ITEMNO,INAME)values (5,'Chocolate'); SQL> select itemno,iname from mv; ITEMNO INAME ---------- ---------- 1 MILK 2 COFFEE 3 JUICE 4 Biscutts 5 Chocolate SQL> select itemno,iname from item; ITEMNO INAME ---------- ---------- 1 MILK 2 COFFEE 3 JUICE 4 Biscutts
  • 71. Dr. Girija Narasimhan The materialized view table “mv” is not matching with “item” base table. Because materialized view stores the copy of the result i.e ROWID is different than ROWID of the item table. So, it is not matching with base table result. To overcome this difficulty, it is necessary to execute refresh method in materialized view.
  • 72. Dr. Girija Narasimhan PART 20 Materialized view Using Aggregate Function
  • 73. In the data warehousing environment the materialized views normally include aggregates. In the materialized view containing aggregates is possible after any type of DML to the base tables. In this example using MIN, MAX, SUM, AVG, COUNT aggregate function.
  • 74. Dr. Girija Narasimhan PART 21 Join Materialized view
  • 75. The Join has two tables. In the example used two tables namely Item and Supplier. The row in the one table always match with row of the another table. Here, no data will be lost i.e it is called lossless join. The table should have Primary key, Foreign Key and Not Null constraints on appropriate Join keys.
  • 76. Dr. Girija Narasimhan PART 22 Materialized view Status
  • 77. Whenever materialized view and base table data’s are synchronized then it is considered that materialized view has fresh data otherwise it has the stale data (out-of-date data). For knowing the status of materialized view data, there is three types of view are available namely DBA_MVEIWS, ALL_MVIEWS, and USER_MVIEWS in the data dictionary. In the data dictionary the columns of the all the three views such as MVIEW_NAEM, STALNESS, LAST_REF, COMPILE_STATE are status are maintain automatically.
  • 78. The STALENESS column display any one value like FRESH, STALE, UNUSABLE, UNKNOWN, UNDEFINED or NEEDS_COMPILE. Whenever NEEDS_COMPILE in these views shows NEEDS_COMPILE OR VALID status. Suppose the STALENESS column value has NEEDS_COMPILE then issue following alter materialized view statement for compile.
  • 79.
  • 80. Dr. Girija Narasimhan A materialized view is a database object. It is physically stored in the database. Because materialized view tables are having local copies of data suppose base table is located remotely. It is also useful to create summary table based on aggregation of the base table data. The materialized view stores both query and the result of a query.
  • 81. Dr. Girija Narasimhan PART 23 Materialized view Build Methods
  • 82. Dr. Girija Narasimhan Materialized view Build Methods BUILD IMMEDIATE BUILD DEFERRED Adding the definition of the materialized view into the schema object in the data dictionary. By Default, create Materialized view + stores the result of the statement. create Materialized view, don’t store result of the materialized view Query execute dbms_mview.refresh('mv2',method=>'c'); For getting materialized view result there is two ways Alter materialized view as refresh complete + enable query rewrite + refresh Alter materialized view mv3 refresh complete enable query rewrite; execute dbms_mview.refresh('mv3');
  • 83. Dr. Girija Narasimhan For creating materialized view, there is two build methods are used in the materialized view. One is BUILD IMMEDIATE and another one is BUILD DEFERRED. The BUILD IMMEDIATE method, it creates the materialized view and adding the definition of the materialized view into the schema object in the data dictionary. And also search the base table according to the query given in the materialized view (i.e SELECT statement) and stores their result in the materialized view.
  • 85. Dr. Girija Narasimhan The build method clause is BUILD DEFERRED, it just creates the materialized view but it don’ search and store the result in the materialized view. This method disables the query rewrite, so first time materialized view is executed by refresh complete method “C” using DBMS_MVIEW_REFRESH statement and then it automatically it will enable the materialized view by specifying the ENABLE QUERY REWRITE clause.