Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
7Activity
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: 714|Likes:
Published by api-3741608

More info:

Published by: api-3741608 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
Alterna

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

SELECT ename, hiredate, sa
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, sa
FROM emp
WHERE ename LIKE NVL (:nam

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

SELECT ename, hiredate, sa
FROM emp
WHERE hiredate BETWEEN TRU

AND TRUNC (SYSDATE) + .9

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

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

AND empno = 7788;

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

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

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

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

HAVING Clause

The HAVING clause filters selected rows only after all rows have been fetched. Using a WHERE clause summing, etc. HAVING clauses should only be used when columns with summary operations applied t clause.

Given Query
Alternat

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 inclu
multicolumn UPDATEs.Separate Subqueries
Combined Subq

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(com
FROM lookup);
EXISTS, NOT IN, Table Joins
Consider the alternatives EXISTS, IN and table joins when doing multiple table joins. None of th
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 d end of a one-to-many relationship. The DISTINCT operator causes Oracle to fetch all rows satis and filter out duplicate values. EXISTS is a faster alternative, because the Oracle optimizer reali 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.
ION ALL
Consider whether a UNION ALL will suffice in place of a UNION. The UNION clause forces all row
UNION to be sorted and merged and duplicates to be filtered before the first row is returned. A
including duplicates and does not have to perform any sort, merge or filter. If your tables are m
duplicate records), or you don't care if duplicates are returned, the UNION ALL is much more eff
UNION
UNION

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 tabl not necessarily faster as it depends on your data and the complexity of the resulting query. Also 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 be used
EMP to return only those rows in DEPT that didn't join to anything in EMP;
SELECT *
FROM dept
WHERE deptno NOT IN (SELECT deptno FROM EMP);
SELECT dept.*
FROM dept, emp
WHERE dept.deptno = emp.deptno (+)
AND emp.ROWID IS NULL;
SELECT *
FROM dept
WHERE NOT EXISTS (SELECT NULL FROM emp WHERE emp.deptno = dept.deptno);
Outer Joins

Activity (7)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
h_sniper551772 liked this
miscr77 liked this
jaganj liked this
swayamji liked this

You're Reading a Free Preview

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