介紹
DBA和開發者都喜歡索引
辨別未使用的索引
Oracle
ALTER INDEX index_name MONITORING USAGE;
要停止監控一個索引
ALTER INDEX index_name NOMONITORING USAGE;
在v$objec_usage視圖中包含有索引監控的使用信息
CREATE OR REPLACE VIEW SYS
(
INDEX_NAME
TABLE_NAME
MONITORING
USED
START_MONITORING
END_MONITORING
)
AS
select io
decode(bitand(i
decode(bitand(ou
ou
ou
from sys
where io
and i
and io
and t
/
COMMENT ON TABLE SYS
/
GRANT SELECT ON SYS
/
該視圖顯示了由數據庫中收集來的索引使用統計
INDEX_NAME: sys
TABLE_NAME: sys
MONITORING: YES (索引正在被監控)
USED: YES (索引已經被使用過)
START_MONITORING: 開始監控的時間
END_MONITORING: 結束監控的時間
所有被使用過至少一次的索引都可以被監控並顯示到這個視圖中
$ cat all_object_usage
CREATE OR REPLACE VIEW SYS
(
OWNER
INDEX_NAME
TABLE_NAME
MONITORING
USED
START_MONITORING
END_MONITORING
)
AS
select u
decode(bitand(i
decode(bitand(ou
ou
ou
from sys
where i
and io
and t
and io
/
COMMENT ON TABLE SYS
/
GRANT SELECT ON SYS
/
CREATE PUBLIC SYNONYM V$ALL_OBJECT_USAGE
FOR SYS
/
每次你使用MONITORING USAGE
辨別數據庫中所有未被使用的索引
這個腳本將會啟動監控所有的索引
##################################################################### ## start_index_monitoring
#####################################################################
#!/bin/ksh
# input parameter:
#
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
!
這個腳本將會停止監控全部的索引:
#####################################################################
## 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
!
這個腳本將會為所有未被使用的索引產生一個報表:
#####################################################################
## 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 < system/$1@$2
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
!
以下就是一個未被使用索引報表的例子:
Unused Indexes Report
OWNER INDEX_NAME TABLE_NAME USE
------------------------------ ------------------------------ ----------------- --- HR DEPT_ID_PK DEPARTMENTS NO
HR DEPT_LOCATION_IX DEPARTMENTS NO
HR EMP_DEPARTMENT_IX EMPLOYEES NO
HR EMP_EMAIL_UK EMPLOYEES NO
HR EMP_EMP_ID_PK EMPLOYEES NO
HR EMP_JOB_IX EMPLOYEES NO
HR EMP_MANAGER_IX EMPLOYEES NO
HR EMP_NAME_IX EMPLOYEES NO
HR JHIST_DEPARTMENT_IX JOB_HISTORY NO
HR JHIST_EMPLOYEE_IX JOB_HISTORY NO
HR JHIST_EMP_ID_ST_DATE_PK JOB_HISTORY NO
HR JHIST_JOB_IX JOB_HISTORY NO
HR JOB_ID_PK JOBS NO
HR LOC_CITY_IX LOCATIONS NO
HR LOC_COUNTRY_IX LOCATIONS NO
HR LOC_ID_PK LOCATIONS NO
HR LOC_STATE_PROVINCE_IX LOCATIONS NO
HR REG_ID_PK REGIONS NO
OE INVENTORY_PK INVENTORIES NO
OE INV_PRODUCT_IX INVENTORIES NO
OE INV_WAREHOUSE_IX INVENTORIES NO
OE ITEM_ORDER_IX ORDER_ITEMS NO
OE ITEM_PRODUCT_IX ORDER_ITEMS NO
OE ORDER_ITEMS_PK ORDER_ITEMS NO
OE ORDER_ITEMS_UK ORDER_ITEMS NO
OE ORDER_PK ORDERS NO
From:http://tw.wingwit.com/Article/program/Oracle/201311/18193.html