Test the index usage of select statements in Oracle

Source: Internet
Author: User

Test the use of indexes for select statements in Oracle -- I have read many articles on SQL Optimization in Oracle and introduced a lot of cases where indexes are not used. I tested some cases when I was free today. -- Test Data 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', 'cler', 'Tom', '123', '1 '); insert into emp values ('02', 'Tom ', 'cler' ', '', '123', '1'); insert into emp values ('03', 'jenny', 'sales', 'pretty ', '123 ', '2'); insert into emp values ('04 ', 'pretty', 'sales', '', '123', '2 '); insert into emp values ('05 ', 'buddy', 'jishu', 'canonicaldy ', '2013', '3'); insert into emp values ('06 ', 'canonicalid', 'jishu', '', '000000', '3'); insert into emp values ('07 ', 'biddy', 'cler ','', '123', '1'); insert into emp values ('08 ', 'biddy', 'cler', '', '123', '3'); comm It; -- test and Result: select * from emp where deptno = 1; -- use the index select * from emp where deptno = '1 '; -- use index (type conversion does not affect index usage) select * from emp where deptno * 2 = 2; -- full table scan (index columns do not use indexes when using functions) select * from emp where deptno = 2/2; -- use the index select * from emp where ename = 'Tom 'and deptno = 1; -- select * from emp where ename = 'Tom 'or deptno = 1; -- full table scan (indexes are used when both or condition columns have indexes) select * from emp where sal! = '0'; -- full table scan (! =, Null, not null do not use the index) select * from emp where mgr = 'Tom '; -- use the index (although the mgr column still uses the null value) select * from emp where deptno in ('1', '2', '3'); -- use index (in use index) select * from emp where job like 'C % '; -- select * from emp where deptno between 1 and 2 using the index (% does not use the index when the first character is used; -- use index -- add a condition where no index is used: When an index is created for multiple columns, the first column of the index does not use the index if it is not in the where clause.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.