0% found this document useful (0 votes)
63 views

Best Practices SQL2

The document discusses best practices for optimizing SQL queries through techniques like: 1. Optimizing the overall process flow by combining or eliminating steps. 2. Using parallel queries and nologging to improve performance by leveraging multiple CPUs and reducing log generation. 3. Using permanent tables instead of temporary tables when possible to allow for statistics and DBA management. 4. Rewriting queries using CASE/DECODE statements or joins instead of subqueries to improve performance.

Uploaded by

kant992
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Best Practices SQL2

The document discusses best practices for optimizing SQL queries through techniques like: 1. Optimizing the overall process flow by combining or eliminating steps. 2. Using parallel queries and nologging to improve performance by leveraging multiple CPUs and reducing log generation. 3. Using permanent tables instead of temporary tables when possible to allow for statistics and DBA management. 4. Rewriting queries using CASE/DECODE statements or joins instead of subqueries to improve performance.

Uploaded by

kant992
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL QUERY TUNING

BEST PRACTICES PART-II


TABLE OF CONTENTS

Optimizing Process Flow ......................................................................................3

PARALLEL and NOLOGGING..................................................................................3

Temporary Tables.................................................................................................3

Case and Decode Statements..............................................................................3

Sub Queries .........................................................................................................4


Optimizing Process Flow

 First step in any tuning exercise is to optimize the process flow.

 Review the process design to identify steps which can be combined or eliminated.

 Identify alternatives which were not considered during development and possible because the
original design may have inherited legacy process flow or did not have a complete picture.

PARALLEL and NOLOGGING

 Parallel operation is the relationship between Memory and the number of CPUs available.

 By means of parallelism, oracle uses multiple processors / processes to execute it more quickly.
The number of parallel execution servers associated with a single operation is known as the
degree of parallelism. The degrees varies from 1 to 10.

 NOLOGGING will help in reduce the generation of the undo or redo logs at the database level.

 Processes running with the NOLOGGING option set run faster because no redo is generated. The
[NO]LOGGING clause applies to tables, partitions, table spaces, and indexes .

e.g. INSERT /*+ PARALLEL (DegreeOfParallelism) */ INTO TABLE;

Temporary Tables

 If a stored procedure creates a temporary table to manage large volumes of data, then this process
can be made more efficient by moving to a permanent table.

 The permanent table can be managed by the DBA and regular statistics can be gathered on it so
that the SQL can pick the correct access path.

 The downside to this is it puts the burden back on the application program to correctly maintain
the table. Logic would need to be added to clean out the portion (or all) of the table that will be
reloaded by this execution of the procedure.

 If the table is a global temporary table, the structure is retained but the data is session specific so
that unless the stored procedure is running, the table would be empty.

Case and Decode Statements


 Case and Decode can be used to take the logic required for multiple SQL steps and perform the
same function in one step.

 The CASE expression syntax is similar to an IF-THEN-ELSE statement. Oracle checks each
condition starting from the first condition (left to right). When a particular condition is satisfied
(WHEN part) the expression returns the tagged value (THEN part). If none of the conditions are
matched, the value mentioned in the ELSE part is returned. The ELSE part of the expression is
not mandatory-- CASE expression will return null if nothing is satisfied.

 The DECODE statement is very similar. In Oracle/PLSQL,the decode function has the
functionality of an IF-THEN-ELSE statement.

Sub Queries

 Sub queries are also known as nested queries and are used to answer multi-part questions.

 Sub queries and joins are often interchangeable and in fact the Oracle optimizer may well treat a
query containing a sub-query exactly as if it were a join.

 Example of rewriting a query would be as follows:

SELECT name FROM emp WHERE dept_no =


(SELECT dept_no FROM emp WHERE name = 'TEMP')

or as a join statement, like this:-

SELECT e1.name FROM emp e1,emp e2 WHERE e1.dept_no = e2.dept_no AND e2name =
'TEMP';

 Use Joins in place of EXISTS.

In general, join tables rather than specifying sub-queries.

SELECT * FROM emp e WHERE EXISTS ( SELECT d.deptno FROM dept d


WHERE e.deptno = d.deptno AND d.dname = 'RESEARCH') ;

To improve performance use the following:

SELECT * FROM emp e, dept d WHERE e.deptno = d.deptno AND d.dname =


'RESEARCH' ;

 Never use NOT on an indexed column. Whenever Oracle encounters a NOT on an index
column, it will perform full-table scan.

SELECT * FROM emp WHERE NOT deptno = 0;


 Never use a function / calculation on an indexed column. If there is any function is used on an
index column, optimizer will not use index. Use some other alternative.

Examples:

Wrong query : SELECT * FROM emp WHERE SUBSTR(ENAME,1,3) = 'ABC' ;

Correct Query : SELECT * FROM emp WHERE ENAME LIKE 'ABC%' ;

You might also like