|
#!/bin/ksh
#index_monitor.sh
# input parameter: 1: connect str, eg. system/manager@orcl
# 2: start|stop
if (($#<1))
then
echo "Please enter connect str as the first parameter, eg. system/manager@orcl !"
exit 0
fi
if (($#<2))
then
echo "Please enter first|start|stop|report as the second parameter!"
exit 0
fi
str=$1
case $2 in
first)
sqlplus '/ as sysdba' << !
CREATE OR REPLACE VIEW SYS.V$ALL_OBJECT_USAGE
(
OWNER,
INDEX_NAME,
TABLE_NAME,
MONITORING,
USED,
START_MONITORING,
END_MONITORING
)
AS
select u.name, io.name, t.name,
decode(bitand(i.flags, 65536), 0, 'NO', 'YES'),
decode(bitand(ou.flags, 1), 0, 'NO', 'YES'),
ou.start_monitoring,
ou.end_monitoring
from sys.obj$ io, sys.obj$ t, sys.ind$ i, sys.object_usage ou, sys.user$ u
where i.obj# = ou.obj#
and io.obj# = ou.obj#
and t.obj# = i.bo#
and io.owner# = u.user#
/
COMMENT ON TABLE SYS.V$ALL_OBJECT_USAGE IS
'Record of all index usage - developed by Daniel Liu'
/
GRANT SELECT ON SYS.V$ALL_OBJECT_USAGE TO "PUBLIC"
/
CREATE PUBLIC SYNONYM V$ALL_OBJECT_USAGE
FOR SYS.V$ALL_OBJECT_USAGE
/
!
;;
start)
sqlplus $str << !
clear columns breaks
set pages 0 feed off wrap off verify off head off trimspool ON colsep ' '
set linesize 100 scan on
set term off
set pagesize 200
spool tmp.sql
select 'ALTER INDEX '||OWNER||'.'||INDEX_NAME||' MONITORING USAGE;' from dba_indexes where owner not in ('CTXSYS','WMSYS','XDB','MDS
YS','ORDSYS','PERFSTAT','QUEST','SYS','SYSTEM','OUTLN','AURORA\$JIS\$UTILITY\$') and index_type <> 'LOB';
spool off
exit
!
cat tmp.sql | sed -e '/^SQL/d' > start_index_monitoring.sql
rm tmp.sql
sqlplus $str << !
@./start_index_monitoring.sql
exit
!
;;
stop)
sqlplus $str << !
clear columns breaks
set pages 0 feed off wrap off verify off head off trimspool ON colsep ' '
set linesize 100 scan on
set term off
set pagesize 200
spool tmp.sql
select 'ALTER INDEX '||OWNER||'.'||INDEX_NAME||' NOMONITORING USAGE;' from dba_indexes where owner not in ('CTXSYS','WMSYS','XDB','M
DSYS','ORDSYS','PERFSTAT','QUEST','SYS','SYSTEM','OUTLN','AURORA\$JIS\$UTILITY\$') and index_type <> 'LOB';
spool off
exit
!
cat tmp.sql | sed -e '/^SQL/d' > stop_index_monitoring.sql
rm tmp.sql
sqlplus $str << !
@./stop_index_monitoring.sql
exit
!
;;
report)
sqlplus $str << !
set feed off
set pagesize 200
set linesize 100
ttitle center "Unused Indexes Report" skip 2
spool unused_index.rpt
select owner,index_name,table_name,used
from v\$all_object_usage
where used = 'NO' and owner not in (
'CTXSYS','WMSYS','XDB','MDSYS','ORDSYS','PERFSTAT','QUEST','SYS','SYSTEM','OUTLN','AURORA\$JIS\$UTILITY\$'
);
spool off
exit
!
;;
*)
echo "Please enter start|stop as the second parameter!"
;;
esac
|