假设这么一个场景,有一张表建了很多索引,我们可以怎么通过观察索引历史使用记录,来看是不是可以删除一些多余索引呢?下面介绍两个实用的脚本:
查看当前索引使用情况
- SELECT p.object_name, p.operation, p.options, COUNT(1)
- FROM v$sql_plan p, v$sql s
- WHERE p.object_owner <> 'SYS'
- AND p.OBJECT_NAME in
- (select index_name
- from dba_indexes
- where table_name = 'S_SHIP_UNIT_LINE')
- AND p.sql_id = s.sql_id
- GROUP BY p.object_name, p.operation, p.options
- ORDER BY 1, 2, 3;
查看历史索引使用情况
- SELECT p.object_name, p.operation, p.options, COUNT(1)
- FROM dba_hist_sql_plan p, dba_hist_sqlstat s
- WHERE p.object_owner <> 'SYS'
- AND p.object_name in
- (select index_name
- from dba_indexes
- where table_name = 'S_SHIP_UNIT_LINE')
- AND p.sql_id = s.sql_id
- GROUP BY p.object_name, p.operation, p.options
- ORDER BY 1, 2, 3;