| |||||
|
Monitoring Index Usage in Oracle9i By: Daniel T. Liu
Introduction
Identifying Unused Indexes
Oracle9i provides a new mechanism of monitoring indexes to determine if those indexes are
being used or not used. To start monitoring an index’s usage, issue this command: CREATE OR REPLACE VIEW SYS.V$OBJECT_USAGE ( INDEX_NAME, TABLE_NAME, MONITORING, USED, START_MONITORING, END_MONITORING ) AS select 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 where io.owner# = userenv('SCHEMAID') and i.obj# = ou.obj# and io.obj# = ou.obj# and t.obj# = i.bo# / COMMENT ON TABLE SYS.V$OBJECT_USAGE IS 'Record of index usage' GRANT SELECT ON SYS.V$OBJECT_USAGE TO "PUBLIC" /
The view displays statistics about index usage gathered from the database. Here are the
descriptions of the view’s columns: $ cat all_object_usage.sql 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 / Each time you issue MONITORING USAGE, the view is reset for the specified index. Any previous usage information is cleared or reset, and a new start time is recorded. Every time you issue NOMONITORING USAGE, no further monitoring is performed; the end time is recorded for the monitoring period. If you drop an index that is being monitored, information about that index will be deleted from V$OBJECT_USAGE or V$ALL_OBJECT_USAGE view.
Identifying All Unused Indexes in a Database #################################################### ## ################################################# #!/bin/ksh # input parameter: 1: password # 2: SID if (($#<1)) then echo "Please enter 'system' user password as the first parameter !" exit 0 fi if (($#<2)) then echo "Please enter instance name as the second parameter!" exit 0 fi sqlplus -s <<! system/$1@$2 set heading off set feed off set pagesize 200 set linesize 100 spool start_index_monitoring.sql select 'ALTER INDEX '||OWNER||'.'||INDEX_NAME||' MONITORING USAGE;' from dba_indexes where owner not in ('SYS','SYSTEM','OUTLN','AURORA\$JIS\$UTILITY\$'); spool off exit ! sqlplus -s <<! oracle/$1@$2 @./start_index_monitoring.sql exit ! This script will stop monitoring of all indexes: ################################################# ## stop_index_monitoring.sh ## ############################################## #!/bin/ksh # input parameter: 1: password # 2: SID if (($#<1)) then echo "Please enter 'system' user password as the first parameter !" exit 0 fi if (($#<2)) then echo "Please enter instance name as the second parameter!" exit 0 fi sqlplus -s <<! system/$1@$2 set heading off set feed off set pagesize 200 set linesize 100 spool stop_index_monitoring.sql select 'ALTER INDEX '||OWNER||'.'||INDEX_NAME||' NOMONITORING USAGE;' from dba_indexes where owner not in ('SYS','SYSTEM','OUTLN','AURORA/$JIS/$UTILITY/$'); spool off exit ! exit sqlplus -s <<! oracle/$1@$2 @./stop_index_monitoring.sql exit ! This script will generate a report for all unused indexes: ####################################### ## identify_unused_index.sh ## #################################### #!/bin/ksh # input parameter: 1: password # 2: SID if (($#<1)) then echo "Please enter 'system' user password as the first parameter !" exit 0 fi if (($#<2)) then echo "Please enter instance name as the second parameter!" exit 0 fi sqlplus -s <<! 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'; spool off exit ! Here is an example of an unused index report: Unused Indexes Report
Conclusion
Other Related Articles ... to read more DBA articles, visit http://dba.fyicenter.com/article/ |
||||