Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
38Activity
0 of .
Results for:
No results containing your search query
P. 1
Oracle SQL Query Tuning Hints

Oracle SQL Query Tuning Hints

Ratings: (0)|Views: 3,700|Likes:
Published by api-3707774

More info:

Published by: api-3707774 on Oct 15, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

Oracle SQL Query Tuning Hints
WHERE Clause
Try to avoid operations on database objects referenced in the WHERE clause.
Given Query
Alternative

SELECT ename, hiredate, sal
FROM emp
WHERE SUBSTR(ename,1,3) = 'SCO';

SELECT ename, hiredate, sal
FROM emp
WHERE ename LIKE 'SCO%';

VARIABLE name VARCHAR2(20)
exec name := 'SCOTT'

SELECT ename, hiredate, sal
FROM emp
WHERE ename = NVL (:name, ename);

VARIABLE name VARCHAR2(20)
exec name := 'SCOTT'

SELECT ename, hiredate, sal
FROM emp
WHERE ename LIKE NVL (:name, '%');

SELECT ename, hiredate, sal
FROM emp
WHERE TRUNC (hiredate) = TRUNC
(SYSDATE);

SELECT ename, hiredate, sal
FROM emp
WHERE hiredate BETWEEN TRUNC

(SYSDATE)
AND TRUNC (SYSDATE) + .99999;

SELECT ename, hiredate, sal
FROM emp
WHERE ename || empno = 'SCOTT7788';

SELECT ename, hiredate, sal
FROM emp
WHERE ename = 'SCOTT

AND empno = 7788;

SELECT ename, hiredate, sal
FROM emp
WHERE sal + 3000 < 5000;

SELECT ename, hiredate, sal
FROM emp
WHERE sal < 2000;

SELECT ename, hiredate, sal
FROM emp
WHERE sal != 0;

SELECT ename, hiredate, sal
FROM emp
WHERE sal > 0;

HAVING Clause

The HAVING clause filters selected rows only after all rows have been fetched. Using a WHERE clause helps reduce overheads in sorting, summing, etc. HAVING clauses should only be used when columns with summary operations applied to them are restricted by the clause.

Given Query
Alternative

SELECT d.dname, AVG (e.sal)
FROM emp e, dept d
WHERE e.deptno = d.deptno
GROUP BY d.dname

HAVING dname != 'RESEAECH'
AND dname != 'SALES';
SELECT d.dname, AVG (e.sal)
FROM emp e, dept d
WHERE e.deptno = d.deptno

AND dname != 'RESEAECH'
AND dname != 'SALES'
GROUP BY d.dname;

Combined Subqueries
Minimize the number of table lookups (subquery blocks) in queries, particularly if your
statements include subquery SELECTs or multicolumn UPDATEs.
Separate Subqueries
Combined Subqueries

SELECT ename
FROM emp
WHERE sal = (SELECT MAX (sal)

FROM lookup)
AND comm = (SELECT MAX (comm)
FROM lookup);

SELECT ename
FROM emp
WHERE (sal,comm) = (SELECT MAX (sal),

MAX(comm)
FROM lookup);
EXISTS, NOT IN, Table Joins
Consider the alternatives EXISTS, IN and table joins when doing multiple table joins. Non
of these are consistently faster; it depends on your data.

SELECT ename
FROM emp E
WHEREEXISTS (SELECT 'X'

FROM dept
WHERE deptno = E.deptno
AND dname = 'ACCOUNTING');

SELECT ename
FROM emp E
WHERE deptnoIN (SELECT deptno

FROM dept
WHERE deptno = E.deptno
AND dname = 'ACCOUNTING');

SELECT ename
FROM dept D, emp E
WHERE E.deptno = D.deptno
AND D.dname = 'ACCOUNTING';

DISTINCT

Avoid joins that require the DISTINCT qualifier on the SELECT list in queries which are
used to determine information at the owner end of a one-to-many relationship. The
DISTINCT operator causes Oracle to fetch all rows satisfying the table join and then sort
and filter out duplicate values. EXISTS is a faster alternative, because the Oracle optimiz
realizes when the subquery has been satisfied once, there is no need to proceed further
and the next matching row can be fetched.

Given Query
Alternative
SELECT DISTINCT d.deptno,
d.dname
FROM dept D,
emp E
WHERE D.deptno = E.deptno;

SELECT d.deptno, d.dname
FROM dept D
WHEREEXISTS (SELECT 'X'

FROM emp E
WHERE E.deptno =
D.deptno);
UNION ALL

Consider whether a UNION ALL will suffice in place of a UNION. The UNION clause forces
all rows returned by each portion of the UNION to be sorted and merged and duplicates t
be filtered before the first row is returned. A UNION ALL simply returns all rows includin
duplicates and does not have to perform any sort, merge or filter. If your tables are
mutually exclusive (include no duplicate records), or you don't care if duplicates are
returned, the UNION ALL is much more efficient.

UNION
UNION ALL

SELECT acct, balance
FROM debit
WHERE trandate = '31-DEC-95'

UNION

SELECT acct, balance
FROM credit
WHERE trandate = '31-DEC-95';

SELECT acct, balance
FROM debit
WHERE trandate = '31-DEC-95'

UNIONALL

SELECT acct, balance
FROM credit
WHERE trandate = '31-DEC-95';

DECODE

Consider using DECODE to avoid having to scan the same rows repetitively or join the
same table repetitively. Note, DECODE is not necessarily faster as it depends on your dat
and the complexity of the resulting query. Also, using DECODE requires you to change
your code when new values are allowed in the field.

SELECT COUNT(*)
FROM emp
WHERE status = 'Y'
AND ename LIKE 'SMITH%';

----------

SELECT COUNT(*)
FROM emp
WHERE status = 'N'
AND ename LIKE 'SMITH%';

SELECT COUNT(DECODE(status, 'Y', 'X', NULL)) Y_count,

COUNT(DECODE(status, 'N', 'X', NULL)) N_count
FROM emp
WHERE ename LIKE 'SMITH%';

Anti Joins

An anti-join is used to return rows from a table that that are present in another table. It might used for example between DEPT and EMP to return only those rows in DEPT that didn't join to anything in EMP;

Activity (38)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Amit Kasana liked this
rohit01234567890 liked this
anushabcd liked this
Himanshu Das liked this
balajismith liked this
balagj70911 liked this
alexandergsam liked this
srasquin liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->