Friday, July 24, 2009

Create and fast refresh materialized view

In 11G, I used below statements to create a materialized view and refresh it.

WHENEVER SQLERROR CONTINUE
drop materialized view erp.db1_CLNCL_INDEX;
--
-- create MVIEW basing on a remote table
--
create materialized view erp.db1_CLNCL_INDEX
tablespace mat_view_data01
build deferred
using index tablespace mat_view_indx01
refresh fast on demand with primary key
as select * from user1.CLNCL_INDEX@db1
  -- where TRUNC("CREATE_TS") > sysdate - 1100
                         -- FAST REFRESH does not like SYSDATE. Error ORA-12015
  where TRUNC("CREATE_TS")  > to_date('01/01/2009', 'MM/DD/YYYY')
;

-- Run "complete" refresh one time, before "fast" refresh. It takes hours.
EXEC DBMS_MVIEW.REFRESH(LIST => 'ERP.DB1_CLNCL_INDEX', METHOD => 'C', ATOMIC_REFRESH => FALSE, PARALLELISM =>'6');
-- Run FAST refresh. It can be put into a shell script to run daily.
EXEC DBMS_MVIEW.REFRESH(LIST => '"ERP"."DB1_CLNCL_INDEX"', METHOD => 'F', PARALLELISM =>'6');

~~~~~~~~~~~~~~~~  Shell script for crontab ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/bin/ksh
## Call sql script to refresh MVs 
# Please specify the variables
export ORACLE_SID=DBNAME
export ORACLE_HOME=/app/oracle/product/11.2.0.3
export PATH=$ORACLE_HOME/bin:${PATH}
export script_dir=/path/to/dba   # Where you save the text log files

Passwd=`XXXXX`
current_dir=`pwd`
cd $script_dir
DT=`date +"%h %d,%y %H:%M"`
BKUPLOG="$script_dir/DBNAME_mvFresh_out.txt"
BKUPLOG_ERR="$script_dir/DBNAME_mvFresh_out_err.txt"

if [ -f $BKUPLOG ]; then
   mv $BKUPLOG ${BKUPLOG}_old
fi
if [ -f $BKUPLOG_ERR ]; then
   mv $BKUPLOG_ERR ${BKUPLOG_ERR}_old
fi

exec 1>$BKUPLOG
exec 2>$BKUPLOG_ERR
echo "Start at $DT"
echo
if [ -f DBNAME_fast_refresh.sql ]; then
   echo "file DBNAME_fast_refresh.sql does exist!!"
else
   echo "file DBNAME_fast_refresh.sql does not exist" |  mailx -s "$script_dir" a1@gmail.com,a2@gmail.com
   exit 1;
fi

$ORACLE_HOME/bin/sqlplus -S /nolog <<-ENDSQL
connect ${Passwd}
show user
@DBNAME_fast_refresh.sql
exit;
ENDSQL

DT=`date +"%h %d,%y %H:%M"`
echo
echo "finish at $DT"
if [ -n "`grep "ORA-" $BKUPLOG`" ] || [ -s $BKUPLOG_ERR ]; then
    cat $BKUPLOG | mailx -s "Error in $script_dir" a1@gmail.com,a2@gmail.com
fi
cd $current_dir
exit
~~~~~~~~~~~~
$ view DBNAME_fast_refresh.sql
spool DBNAME_MVrefresh.lst
EXEC DBMS_MVIEW.REFRESH(LIST => '"ERP"."DB1_CLNCL_INDEX"', METHOD => 'F', PARALLELISM =>'6');

select '1', to_char(sysdate, 'dd/mm/yyyy hh24:mi:ss') datetime from dual;
-- put more statements here to refresh more MVWs 
spool off

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In 10G, below SQL is helpful for finding the materialized view and the master table from the MLOG name.

SQL> select distinct
ds.master_owner "MasterTable owner",
ds.master "MasterTable name",
ds.owner "MView owner", ds.name "Snapshot/Mview name",
dsl.log_owner "Log owner", dsl.log_table "MLog name"
FROM dba_snapshot_logs dsl, dba_snapshots ds
WHERE dsl.log_table like '%MLOG$_BOM_OPERATION_SEQUEN%'
and ds.master = dsl.master
--and ds.owner = dsl.log_owner
order by 3;

Note that the information in DBA_SNAPSHOT may not be accurate. For example, Materialized view ENI_DBI_MFG_STEPS_JOIN_MV is created by a join of bom.bom_operational_routings and bom.bom_operation_sequences. But, DBA_SNAPSHOT only reports table BOM_OPERATION_SEQUENCES as the master.

DBA_MVIEWS does not have the MASTER column.

No comments: