对某张表的索引监控记录

标识索引的使用情况  
1.启用索引监控  
  alter index emp_ename_idx monitoring usage;  
2.执行相关查询  
 select ename,job ,sal from scott.emp  
   where ename like 'C%';  
3.查看索引是否使用  
select * from v$object_usage;  
4.禁用索引监控  
 alter index emp_ename_idx nomonitoring usage;  

索引的操作
建立索引
CREATE INDEX
CREATE [unique] INDEX [user.]index
ON [user.]table (column [ASC | DESC] [,column
[ASC | DESC] ] ... )
[CLUSTER [scheam.]cluster]
[INITRANS n]
[MAXTRANS n]
[PCTFREE n]
[STORAGE storage]
[TABLESPACE tablespace]
[NO SORT]
Advanced

修改索引
ALTER [UNIQUE] INDEX [user.]index
[INITRANS n]
[MAXTRANS n] 
REBUILD 
[STORAGE n]

删除索引
DROP INDEX [schema.]indexname

禁用索引
alter index fk_deptno unusable;

SQL查看索引
当前session使用索引的记录
select 'select '''||index_name||'''as index_name,count(1) as session_count 
from v$sql_plan where object_name like ''%'||index_name||'%''
union all'
from dba_indexes
where table_name='T_RESV_BASE';

历史中使用索引的记录
select 'select '''||index_name||''' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
''%'||index_name||'%''
union all'
from dba_indexes
where table_name='T_RESV_BASE';

整理结果执行查询
with
his_count as (
select 'RESV_IDX_BOOKUSER' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_BOOKUSER%'
union all
select 'RESV_IDX_PID' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_PID%'
union all
select 'RESV_IDX_GUASTATUS' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_GUASTATUS%'
union all
select 'RESV_IDX_PRESTATUS' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_PRESTATUS%'
union all
select 'RESV_IDX_PMSNO' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_PMSNO%'
union all
select 'RESV_STATUS_TIME_INDEX' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_STATUS_TIME_INDEX%'
union all
select 'PK_T_R3' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%PK_T_R3%'
union all
select 'RESV_CNF_INDEX' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_CNF_INDEX%'
union all
select 'RESV_OUTCNF_INDEX' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_OUTCNF_INDEX%'
union all
select 'RESV_SEND_INDEX' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_SEND_INDEX%'
union all
select 'RESV_RP_INDEX' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_RP_INDEX%'
union all
select 'RESV_UPDATETIMESTAMP' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_UPDATETIMESTAMP%'
union all
select 'IDX_RESV_FLAG' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%IDX_RESV_FLAG%'
union all
select 'RESV_FREEEZE_IDX' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_FREEEZE_IDX%'
union all
select 'RESV_IDX_FIRSTCONFIRMTIME' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_FIRSTCONFIRMTIME%'
union all
select 'RESV_IDX_BOOKDATE' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_BOOKDATE%'
union all
select 'RESV_IDX_INDATE' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_INDATE%'
union all
select 'RESV_IDX_OUTDATE' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_OUTDATE%'
union all
select 'RESV_IDX_SLUICE' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_SLUICE%'
union all
select 'RESV_CHECK_OUTCNFNUM' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_CHECK_OUTCNFNUM%'
union all
select 'RESV_IDX_BOOKDAY' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_BOOKDAY%'
union all
select 'RESV_IDX_INDAY' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_INDAY%'
union all
select 'RESV_IDX_RATECODE' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_RATECODE%'
union all
select 'RESV_HOLDTIME_IDX' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_HOLDTIME_IDX%'
union all
select 'RESV_ACPAYMENT_IDX' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_ACPAYMENT_IDX%'
union all
select 'RESV_IDX_PROP_IATA' as index_name,count(1) as his_count
  FROM dba_hist_active_sess_history a
  JOIN dba_hist_sqltext b
    ON a.sql_id = b.sql_id
  JOIN DBA_HIST_SQL_PLAN c
    ON a.sql_id = c.sql_id
    where c.object_name like
'%RESV_IDX_PROP_IATA%'
),
session_count as (
select 'RESV_IDX_BOOKUSER'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_BOOKUSER%'
union all
select 'RESV_IDX_PID'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_PID%'
union all
select 'RESV_IDX_GUASTATUS'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_GUASTATUS%'
union all
select 'RESV_IDX_PRESTATUS'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_PRESTATUS%'
union all
select 'RESV_IDX_PMSNO'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_PMSNO%'
union all
select 'RESV_STATUS_TIME_INDEX'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_STATUS_TIME_INDEX%'
union all
select 'PK_T_R3'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%PK_T_R3%'
union all
select 'RESV_CNF_INDEX'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_CNF_INDEX%'
union all
select 'RESV_OUTCNF_INDEX'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_OUTCNF_INDEX%'
union all
select 'RESV_SEND_INDEX'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_SEND_INDEX%'
union all
select 'RESV_RP_INDEX'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_RP_INDEX%'
union all
select 'RESV_UPDATETIMESTAMP'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_UPDATETIMESTAMP%'
union all
select 'IDX_RESV_FLAG'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%IDX_RESV_FLAG%'
union all
select 'RESV_FREEEZE_IDX'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_FREEEZE_IDX%'
union all
select 'RESV_IDX_FIRSTCONFIRMTIME'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_FIRSTCONFIRMTIME%'
union all
select 'RESV_IDX_BOOKDATE'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_BOOKDATE%'
union all
select 'RESV_IDX_INDATE'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_INDATE%'
union all
select 'RESV_IDX_OUTDATE'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_OUTDATE%'
union all
select 'RESV_IDX_SLUICE'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_SLUICE%'
union all
select 'RESV_CHECK_OUTCNFNUM'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_CHECK_OUTCNFNUM%'
union all
select 'RESV_IDX_BOOKDAY'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_BOOKDAY%'
union all
select 'RESV_IDX_INDAY'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_INDAY%'
union all
select 'RESV_IDX_RATECODE'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_RATECODE%'
union all
select 'RESV_HOLDTIME_IDX'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_HOLDTIME_IDX%'
union all
select 'RESV_ACPAYMENT_IDX'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_ACPAYMENT_IDX%'
union all
select 'RESV_IDX_PROP_IATA'as index_name,count(1) as session_count
from v$sql_plan where object_name like '%RESV_IDX_PROP_IATA%'
)
select a.index_name,a.his_count his_count,b.session_count session_count
from his_count a,session_count b
where a.index_name=b.index_name
order by his_count,session_count


将结果导入数据库,如下sql提供查询

SELECT
  tab_index_17.id1,
  tab_index_17.his_count,
  tab_index_17.session_count
FROM
  (SELECT DISTINCT
    aaa.id1,
    aaa.his_count,
    aaa.session_count
  FROM
    (SELECT
      id1,
      IFNULL(id2, 0) + IFNULL(id3, 0) + IFNULL(id4, 0) AS his_count,
      IFNULL(id5, 0) + IFNULL(id6, 0) AS session_count
    FROM
      index_17
    WHERE id1 IN (
        'RESV_IDX_PID',
        'RESV_ACPAYMENT_IDX',
        'RESV_HOLDTIME_IDX',
        'RESV_IDX_RATECODE',
        'RESV_IDX_INDAY',
        'RESV_IDX_BOOKDAY',
        'RESV_CHECK_OUTCNFNUM',
        'RESV_IDX_SLUICE ',
        'IDX_RESV_FLAG',
        'RESV_RP_INDEX',
        'RESV_IDX_PMSNO',
        'RESV_IDX_PRESTATUS',
        'RESV_IDX_BOOKUSER',
        'RESV_IDX_OUTDATE',
        'RESV_SEND_INDEX',
        'PK_T_R3',
        'RESV_CNF_INDEX',
        'RESV_OUTCNF_INDEX',
        'RESV_IDX_GUASTATUS',
        'RESV_IDX_INDATE',
        'RESV_FREEEZE_IDX',
        'RESV_UPDATETIMESTAMP',
        'RESV_STATUS_TIME_INDEX',
        'RESV_IDX_BOOKDATE',
        'RESV_IDX_PROP_IATA'
      )) AS aaa
  WHERE aaa.his_count = 0
    AND aaa.session_count = 0) AS tab_index_17,
  (SELECT DISTINCT
    aaa.id1,
    aaa.his_count,
    aaa.session_count
  FROM
    (SELECT
      id1,
      IFNULL(id2, 0) + IFNULL(id3, 0) + IFNULL(id4, 0) AS his_count,
      IFNULL(id5, 0) + IFNULL(id6, 0) AS session_count
    FROM
      index_20
    WHERE id1 IN (
        'RESV_IDX_PID',
        'RESV_ACPAYMENT_IDX',
        'RESV_HOLDTIME_IDX',
        'RESV_IDX_RATECODE',
        'RESV_IDX_INDAY',
        'RESV_IDX_BOOKDAY',
        'RESV_CHECK_OUTCNFNUM',
        'RESV_IDX_SLUICE ',
        'IDX_RESV_FLAG',
        'RESV_RP_INDEX',
        'RESV_IDX_PMSNO',
        'RESV_IDX_PRESTATUS',
        'RESV_IDX_BOOKUSER',
        'RESV_IDX_OUTDATE',
        'RESV_SEND_INDEX',
        'PK_T_R3',
        'RESV_CNF_INDEX',
        'RESV_OUTCNF_INDEX',
        'RESV_IDX_GUASTATUS',
        'RESV_IDX_INDATE',
        'RESV_FREEEZE_IDX',
        'RESV_UPDATETIMESTAMP',
        'RESV_STATUS_TIME_INDEX',
        'RESV_IDX_BOOKDATE',
        'RESV_IDX_PROP_IATA'
      )) AS aaa
  WHERE aaa.his_count = 0
    AND aaa.session_count = 0) AS tab_index_20,
  (SELECT DISTINCT
    aaa.id1,
    aaa.his_count,
    aaa.session_count
  FROM
    (SELECT
      id1,
      IFNULL(id2, 0) + IFNULL(id3, 0) + IFNULL(id4, 0) AS his_count,
      IFNULL(id5, 0) + IFNULL(id6, 0) AS session_count
    FROM
      index_21
    WHERE id1 IN (
        'RESV_IDX_PID',
        'RESV_ACPAYMENT_IDX',
        'RESV_HOLDTIME_IDX',
        'RESV_IDX_RATECODE',
        'RESV_IDX_INDAY',
        'RESV_IDX_BOOKDAY',
        'RESV_CHECK_OUTCNFNUM',
        'RESV_IDX_SLUICE ',
        'IDX_RESV_FLAG',
        'RESV_RP_INDEX',
        'RESV_IDX_PMSNO',
        'RESV_IDX_PRESTATUS',
        'RESV_IDX_BOOKUSER',
        'RESV_IDX_OUTDATE',
        'RESV_SEND_INDEX',
        'PK_T_R3',
        'RESV_CNF_INDEX',
        'RESV_OUTCNF_INDEX',
        'RESV_IDX_GUASTATUS',
        'RESV_IDX_INDATE',
        'RESV_FREEEZE_IDX',
        'RESV_UPDATETIMESTAMP',
        'RESV_STATUS_TIME_INDEX',
        'RESV_IDX_BOOKDATE',
        'RESV_IDX_PROP_IATA'
      )) AS aaa
  WHERE aaa.his_count = 0
    AND aaa.session_count = 0) AS tab_index_21,
  (SELECT DISTINCT
    aaa.id1,
    aaa.his_count,
    aaa.session_count
  FROM
    (SELECT
      id1,
      IFNULL(id2, 0) + IFNULL(id3, 0) + IFNULL(id4, 0) AS his_count,
      IFNULL(id5, 0) + IFNULL(id6, 0) AS session_count
    FROM
      index_22
    WHERE id1 IN (
        'RESV_IDX_PID',
        'RESV_ACPAYMENT_IDX',
        'RESV_HOLDTIME_IDX',
        'RESV_IDX_RATECODE',
        'RESV_IDX_INDAY',
        'RESV_IDX_BOOKDAY',
        'RESV_CHECK_OUTCNFNUM',
        'RESV_IDX_SLUICE ',
        'IDX_RESV_FLAG',
        'RESV_RP_INDEX',
        'RESV_IDX_PMSNO',
        'RESV_IDX_PRESTATUS',
        'RESV_IDX_BOOKUSER',
        'RESV_IDX_OUTDATE',
        'RESV_SEND_INDEX',
        'PK_T_R3',
        'RESV_CNF_INDEX',
        'RESV_OUTCNF_INDEX',
        'RESV_IDX_GUASTATUS',
        'RESV_IDX_INDATE',
        'RESV_FREEEZE_IDX',
        'RESV_UPDATETIMESTAMP',
        'RESV_STATUS_TIME_INDEX',
        'RESV_IDX_BOOKDATE',
        'RESV_IDX_PROP_IATA'
      )) AS aaa
  WHERE aaa.his_count = 0
    AND aaa.session_count = 0) AS tab_index_22,
  (SELECT DISTINCT
    aaa.id1,
    aaa.his_count,
    aaa.session_count
  FROM
    (SELECT
      id1,
      IFNULL(id2, 0) + IFNULL(id3, 0) + IFNULL(id4, 0) AS his_count,
      IFNULL(id5, 0) + IFNULL(id6, 0) AS session_count
    FROM
      index_25
    WHERE id1 IN (
        'RESV_IDX_PID',
        'RESV_ACPAYMENT_IDX',
        'RESV_HOLDTIME_IDX',
        'RESV_IDX_RATECODE',
        'RESV_IDX_INDAY',
        'RESV_IDX_BOOKDAY',
        'RESV_CHECK_OUTCNFNUM',
        'RESV_IDX_SLUICE ',
        'IDX_RESV_FLAG',
        'RESV_RP_INDEX',
        'RESV_IDX_PMSNO',
        'RESV_IDX_PRESTATUS',
        'RESV_IDX_BOOKUSER',
        'RESV_IDX_OUTDATE',
        'RESV_SEND_INDEX',
        'PK_T_R3',
        'RESV_CNF_INDEX',
        'RESV_OUTCNF_INDEX',
        'RESV_IDX_GUASTATUS',
        'RESV_IDX_INDATE',
        'RESV_FREEEZE_IDX',
        'RESV_UPDATETIMESTAMP',
        'RESV_STATUS_TIME_INDEX',
        'RESV_IDX_BOOKDATE',
        'RESV_IDX_PROP_IATA'
      )) AS aaa
  WHERE aaa.his_count = 0
    AND aaa.session_count = 0) AS tab_index_25
WHERE tab_index_17.id1 = tab_index_17.id1
  AND tab_index_17.id1 = tab_index_20.id1
  AND tab_index_17.id1 = tab_index_21.id1
  AND tab_index_17.id1 = tab_index_22.id1
  AND tab_index_20.id1 = tab_index_21.id1
  AND tab_index_20.id1 = tab_index_22.id1
  AND tab_index_20.id1 = tab_index_25.id1
  AND tab_index_21.id1 = tab_index_22.id1
  AND tab_index_21.id1 = tab_index_25.id1
  AND tab_index_25.id1 = tab_index_25.id1

你可能感兴趣的:(对某张表的索引监控记录)