当前位置:数据库 > Oracle >>

Oracle中select语句使用索引情况测试

Oracle中select语句使用索引情况测试
 
--看了不少Oracle中sql优化的文章,也介绍了很多不使用索引的情况,今天有空就测试了一下部分情况。 
--测试数据 
create table EMP 
  EMPNO  VARCHAR2(10) not null primary key, 
  ENAME  VARCHAR2(10), 
  JOB    VARCHAR2(10), 
  MGR    VARCHAR2(10), 
  SAL    NUMBER(10), 
  DEPTNO NUMBER(10) 
)
create index I_DEPTNO on EMP (DEPTNO); 
create index I_JOB on EMP (JOB); 
create index I_MGR on EMP (MGR); 
create index I_SAL on EMP (SAL);
insert into emp values ('01','jacky','clerk','tom','1000','1'); 
insert into emp values ('02','tom','clerk','','2000','1'); 
insert into emp values ('03','jenny','sales','pretty','600','2'); 
insert into emp values ('04','pretty','sales','','800','2'); 
insert into emp values ('05','buddy','jishu','canndy','1000','3'); 
insert into emp values ('06','canndy','jishu','','1500','3'); 
insert into emp values ('07','biddy','clerk','','2000','1'); 
insert into emp values ('08','biddy','clerk','','2000','3'); 
commit;
 
--测试及结果: 
select * from emp where deptno = 1; 
--使用索引 
select * from emp where deptno = '1'; 
--使用索引(类型转换不影响索引使用)
select * from emp where deptno*2 = 2; 
--全表扫描(索引列使用函数时不使用索引) 
select * from emp where deptno = 2/2; 
--使用索引
select * from emp where ename = 'tom' and deptno = 1; 
--使用索引 
select * from emp where ename = 'tom' or deptno = 1; 
--全表扫描 (当or条件列都存在索引时会使用索引)
select * from emp where sal != '0'; 
--全表扫描(!=,null,not null都不使用索引)
select * from emp where mgr = 'tom'; 
--使用索引(虽然mgr列存在null值还是使用了索引)
select * from emp where deptno in ('1','2','3'); 
--使用索引(in使用索引)
select * from emp where job like 'c%'; 
--使用索引(%在第一个字符时不使用索引)
select * from emp where deptno between 1 and 2; 
--使用索引
--补充一个不使用索引的情况:多列创建索引时,索引第一列不在where中则不使用索引。
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,