有没有办法在执行查询时查找Oracle是否曾使用过特定的Oracle索引?
我们有一个基于函数的索引,我怀疑它没有被Oracle使用,因此一些查询运行缓慢。如何判断对数据库运行的查询是否正在使用此查询?
答案 0 :(得分:7)
如果问题是:是否有任何查询曾使用过索引?
ALTER INDEX myindex MONITORING USAGE;
等几天/几个月/年:
SELECT *
FROM v$object_usage
WHERE index_name = 'MYINDEX';
http://docs.oracle.com/cd/B28359_01/server.111/b28310/indexes004.htm#i1006905
答案 1 :(得分:5)
如果你正在使用某种类型的IDE(例如Oracle的SQL Developer,来自Allround Automations的PL / SQL Developer,Toad等),他们每个人都有一些方法来转储语句的计划 - 在菜单中查找和在线帮助。
如果你可以进入SQL * Plus(尝试在友好的命令行输入“sql”),你可以打开autotrace,执行你的语句,然后打印计划。如在
SQL> set autotrace on
SQL> select * from dept where deptno = 40;
DEPTNO DNAME LOC
---------- -------------- -------------
40 OPERATIONS BOSTON
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=1 Bytes=18)
1 0 TABLE ACCESS (BY INDEX ROWID) OF 'DEPT' (Cost=1 Card=1 Bytes=18)
2 1 INDEX (UNIQUE SCAN) OF 'PK_DEPT' (UNIQUE)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
499 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
这假设您的友好邻居DBA已执行必要的咒语以启用此功能。如果还没有这样做,或者您只是想要One More Way(tm)来做这件事,请尝试以下内容,替换您关心的查询:
SQL> EXPLAIN PLAN FOR select * from dept where deptno = 40;
Explained.
SQL> set linesize 132
SQL> SELECT * FROM TABLE( dbms_xplan.display);
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------
Plan hash value: 2852011669
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 20 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 20 | 1 (0)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | PK_DEPT | 1 | | 0 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("DEPTNO"=40)
14 rows selected.
分享并享受。