oracle order by索引是否使用的情况
刚开始在网上看了很多说order by 走索引有两个前提条件
下面是测试: 建表: create table person ( id VARCHAR2(50) not null,name VARCHAR2(20) not null,phone VARCHAR2(11) not null,time TIMESTAMP(6) not null )
创建索引 create index index_person on person(time,phone);
执行语句测试: 1, select * from person where 1=1 and time <= to_date('2013-12-12 12:12:12','yyyy-MM-dd hh24:mi:ss') and phone = '13892441321' order by time desc --使用索引
2, select * from person order by time desc --不使用索引
这个有个特别的情况: 1, select * from person where 1=1 and phone = '13892441321' order by time desc --使用索引
2, select * from person where 1=1 and phone = '13892441321' --不使用索引
这种情况是加了个 分页的时候使用索引,例如: select * from person order by time desc --不使用索引
2, select * from ( select * from person order by time desc) --这里换成order by phone也是不走索引的,这个是索引的前缀原则。 where rownum <=1000 --使用索引
总结:其实 假如一定要对使用函数的列启用索引, CREATE INDEX EMP_I ON EMP (UPPER(ename)); /*建立基于函数的索引*/
SELECT * FROM emp WHERE UPPER(ename) = ‘BLACKSNAIL'; /*将使用索引*/
5 怎样监控无用的索引 Oracle 9i以上,可以监控索引的使用情况,假如一段时间内没有使用的索引,一般就是无用的索引 语法为: 开始监控:alter index index_name monitoring usage;
检查使用状态:select * from v$object_usage;
停止监控:alter index index_name nomonitoring usage;
当然,假如想监控整个用户下的索引,可以采用如下的脚本: set heading off set echo off set feedback off set pages 10000 spool start_index_monitor.sql SELECT 'alter index 'owner'.'index_name' monitoring usage;' FROM dba_indexes WHERE owner = USER; spool off set heading on set echo on set feedback on set heading off set echo off set feedback off set pages 10000 spool stop_index_monitor.sql SELECT 'alter index 'owner'.'index_name' nomonitoring usage;' FROM dba_indexes WHERE owner = USER; spool off set heading on set echo on set feedback on
其实最好的办法是查看执行计划。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |