Oracle+sql Optimization Examples

Source: Internet
Author: User

1. Reduce I/O operations:

SELECT count ( case if empno>then 1 END) C1, Count empno<then 1 END) C2
from EMP;
2. Access via ROWID

SELECT ROWID,emp.* from emp
WHERE Rowid=chartorowid (' aaahw7aabaaamuiaaa ')

3. Use index unique scan

SELECT empno,ename from emp
WHERE empno=' 2000 '

4. Using and connecting symbols causes Oracle to ignore the use of the lasso, even if it is a unique index

SELECT empno,ename from emp
WHERE empno| | ename=' 2000naem '

You can use the index if you change it.

SELECT * from EMP
WHERE empno=+ ename=' dd '

5. Index Range Scan

SELECT * from EMP
WHERE empno<7000

6. the parse order of the WHERE conditional clause is from bottom to top

SELECT a.empno,b.dname from emp a,DEPTB
WHERE a.ename<' Clerk '
and A.deptno=B.deptno;

Takes 1.016 seconds

SELECT a.empno,b.dname from emp a,dept b
WHERE A.deptno=B.deptno
and A.ename<' clerk ';

time 0.813 seconds

7. Using wildcards will allow Oracle to not use the index

SELECT ename from emp
WHERE ename like '%c% '

should be changed into

SELECT ename from emp
WHERE ename like ' c% '

8. Finding the exact value with a unique index is the quickest, and the index range scan is more suitable for finding >=,<= data

SELECT A.ItemID
from Pt_sche_detail a,
Pt_post_role b
WHERE A.ItemID = b.taskid
and A.docid = 2281
and A.ItemID! = 1169015
and a.status! = 0
and B.posttype = 1
and B.roleid = 1022
and B.roletype = 1

The above statement is changed to:

SELECTA.ItemID
fromPt_sche_detail A,
Pt_post_role b
WHEREA.ItemID=B.taskid
andA.docid=2281
andA.ItemID!=1169015
andA.status!=0
andB.taskidinch
(SELECTItemid
fromPt_sche_detail Temp
WHERETemp.docid=2281
andrownum <= (SELECT COUNT (Itemid) fromPt_sche_detail TempWHERETemp.docid=2281))
andB.roleid=1022
andB.roletype=1
andB.posttype=1

Oracle+sql Optimization Examples

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.