3
Best Basic Practices Verify optimal index usage - This is one of the most important SQL best practices where you examine your SQL and verify that your SQL is using the most selective. Use minus instead of EXISTS sub-queries – Use of minus operator instead of NOT IN or NOT exists will usually result in faster execution. E.g. Fast duplicate record deletion. Use SQL analytic functions – The oracle analytic functions can do multiple aggregations (e.g. rollup by cube) with a single pass through the tables, making them very fast for reporting/COUNTING SQL. Re-write Not Exists and Not exists sub-queries as outer joins – In many cases of NOT queries (but ONLY where a column is defined as null), you can re-write the uncorrelated sub-queries into outer joins where is null tests. Not that this is a non-correlated sub-query, but it could be written as an outer join. Remove unnecessary large-table full-table scans - Unnecessary full-table scans cause a huge amount of unnecessary I/O and can drag-down an entire database and it's a SQL best practice to identify unnecessary full scans and remove them by adding indexes. Cache small-table full-table scans - Caching is a SQL best practice in cases where a dedicated data buffer is available for caching table rows. CASE o Use case statements instead of Decode when possible for better readability and maintainability. Remove any unused variables from script.. Avoid using names like "i" and "j" for variables. Sure, we all pretty much know what they mean (integer iterators, for the most part), but they are still obscure and do not enhance the readability of my code. Try to avoid repeating variable names at different scopes (especially nested and especially parameters). Rewrite complex sub-queries with temporary tables – Oracle created the global temporary tables (GTT) and the SQL WITH operator to help divide-and –conquer complex

ORACLE Basic Best Practices

Embed Size (px)

Citation preview

Page 1: ORACLE Basic Best Practices

Best Basic Practices

Verify optimal index usage - This is one of the most important SQL best practices where you examine your SQL and verify that your SQL is using the most selective. 

Use minus instead of EXISTS sub-queries – Use of minus operator instead of NOT IN or NOT exists will usually result in faster execution. E.g. Fast duplicate record deletion.

Use SQL analytic functions – The oracle analytic functions can do multiple aggregations (e.g. rollup by cube) with a single pass through the tables, making them very fast for reporting/COUNTING SQL.

Re-write Not Exists and Not exists sub-queries as outer joins – In many cases of NOT queries (but ONLY where a column is defined as null), you can re-write the uncorrelated sub-queries into outer joins where is null tests. Not that this is a non-correlated sub-query, but it could be written as an outer join.

Remove unnecessary large-table full-table scans - Unnecessary full-table scans cause a huge amount of unnecessary I/O and can drag-down an entire database and it's a SQL best practice to identify unnecessary full scans and remove them by adding indexes. 

Cache small-table full-table scans - Caching is a SQL best practice in cases where a dedicated data buffer is available for caching table rows.  

CASEo Use case statements instead of Decode when possible for better readability and

maintainability.

Remove any unused variables from script..

 Avoid using names like "i" and "j" for variables. Sure, we all pretty much know what they mean (integer iterators, for the most part), but they are still obscure and do not enhance the readability of my code.

Try to avoid repeating variable names at different scopes (especially nested and especially parameters).

Rewrite complex sub-queries with temporary tables – Oracle created the global temporary tables (GTT) and the SQL WITH operator to help divide-and –conquer complex sub-queries (especially those with where clause sub-queries, SELECT cause scalar sub-queries and FROM clause in-line views). Tuning SQL with temporary tables ( and materializations in the WITH clause) can result in amazing performance improvements.

Avoid hard-coding and repetition. 

Boolean variables and functions allow you to greatly improve readability of programs.   

Avoid Unstructured Exits from Loops

o Do not  EXIT or RETURN out of a FOR loop. o A FOR loop should only be used when you want to execute the body a fixed

number of times.

Page 2: ORACLE Basic Best Practices

o Do not use the EXIT syntax in a WHILE loop.o The loop should be terminated only when the condition in the boundary

evaluates to FALSE.

Use separate line for each expression in a SELECT list Place each table in a FROM clause on its own line Place each expression in WHERE clause on its own line Use sensible abbreviations for table and column aliases UPPER all oracle reserve words and right align them Lower all object words Keep beginning and ending of multi-line statements equally indented Don't repeat the same logical SQL statement.

o Repetition makes it almost impossible to maintain and optimize. Use ROWNUM in WHERE CLAUSE especially when using EXISTS condition

SELECT  ‘X’ from SGBSTDN WHERE …. In statements with multiple key words, attempt to right align the key words, for

example:

SELECT DISTINCT last_name               ,first_name               ,middle_init          FROM l0_empl_directory         WHERE first_name IS NOT NULL             AND UPPER (last_name) LIKE UPPER ('S%')       ORDER BY last_name         ,first_name               ,middle_init; 

BULK COLLECTo Use with implicit and explicit queries.o Recurring SQL statement in PL/SQL loop. Oracle recommended threshold:

five rows!o Things to be aware of:

You MUST know how to use collections to use this feature! Only a single DML statement is allowed per FORALL. SQL%BULK_ROWCOUNT returns the number of rows affected by

each row in the binding array. Use SAVE EXCEPTIONS to continue past errors since If error occurs, prior successful DML statements are NOT ROLLED

BACK Use standard comments for version tracking Only one statement per line Appropriate grants have to be incorporated into the scripts

Note :-

Performance Analysis and comparison toolo TKPROFo SQL*Plus SET TIMING ONo DBMS_UTILITY.GET_TIME/GET_CPU_TIMEo PL/SQL or TOAD utilities for explain plan.o DBMS_PROFILER

Page 3: ORACLE Basic Best Practices

o ORASRPo HOTSOS Profiler