Best Practices SQL2
Best Practices SQL2
Temporary Tables.................................................................................................3
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 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 .
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.
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.
SELECT e1.name FROM emp e1,emp e2 WHERE e1.dept_no = e2.dept_no AND e2name =
'TEMP';
Never use NOT on an indexed column. Whenever Oracle encounters a NOT on an index
column, it will perform full-table scan.
Examples: