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

Linux大全评论888 views阅读模式

--看了不少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%';
--使用索引(%在第一个字符时不使用索引)

企鹅博客
  • 本文由 发表于 2019年9月13日 07:19:38
  • 转载请务必保留本文链接:https://www.qieseo.com/183160.html

发表评论