小编给大家分享一下Oracle中如何找出有哪些索引从来没有被使用过,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
数据库经过N手后,索引乱建,下面的SQL就找出一点时间范围内哪些索引从来没被使用过,亲测好用,注意一点,生成的SQL你要是执行了,就会从脚本中消失了,
SELECT X.OWNER,
X.TABLE_NAME,
X.INDEX_NAME,
C.COLUMN_POSITION,
C.COLUMN_NAME,
S.BYTES / 1024 / 1024 M,
q'[select count(*),count(distinct ]' || C.COLUMN_NAME ||
q'[) from ]' || X.OWNER || '.' || X.TABLE_NAME || q'[;]' AS QUERY_Q
FROM (SELECT A.OWNER, A.TABLE_NAME, A.INDEX_NAME
FROM DBA_INDEXES A
WHERE A.OWNER IN (SELECT USERNAME
FROM DBA_USERS
WHERE CREATED > DATE '2018-10-30')
AND (A.OWNER, A.INDEX_NAME) NOT IN
(SELECT
B.OBJECT_OWNER, B.OBJECT_NAME
FROM GV$SQL A, GV$SQL_PLAN B
WHERE A.SQL_ID = B.SQL_ID
AND A.CHILD_NUMBER = B.CHILD_NUMBER
AND B.OBJECT_OWNER IN
(SELECT USERNAME
FROM DBA_USERS
WHERE CREATED > DATE '2018-10-30')
AND B.OBJECT_TYPE LIKE '%INDEX%'
AND B.TIMESTAMP > DATE '2018-10-10')
AND A.TABLE_NAME NOT LIKE 'SYS%'
AND A.UNIQUENESS <> 'UNIQUE') X
INNER JOIN DBA_IND_COLUMNS C
ON C.INDEX_OWNER = X.OWNER
AND C.INDEX_NAME = X.INDEX_NAME
AND C.TABLE_NAME = X.TABLE_NAME
INNER JOIN DBA_SEGMENTS S
ON S.SEGMENT_NAME = C.TABLE_NAME
AND S.OWNER = C.INDEX_OWNER
AND S.BYTES / 1024 / 1024 > 10
ORDER BY 1, 2, 3, 4;
以上是“Oracle中如何找出有哪些索引从来没有被使用过”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!