Best Practices - Oracle SQL - 1
Best Practices - Oracle SQL - 1
BEST PRACTICES
TABLE OF CONTENTS
1.
2.
3.
Object creation............................................................................................. 3
4.
Multi-Threading Processes............................................................................3
5.
2.
Common reasons for not utilizing indexes are because of the use of functions in the indexes.
LEFT SIDE of the search condition
Some examples are given below:
Implicit conversion
NVL to handle null columns
UPPER to handle case insensitive searches
Other possible reasons may include statistics on the tables and also the optimizer mode used. This
is related to the optimizer statistics and the different kinds of optimizer mode used at the database
level.
The optimizer will decide not to use the index if the where clause predicates for SQL statements
visit more data blocks.
In the where clauses, if the column data type is a number, do not use single quotes. Likewise, if
the column data type is varchar2 always use single quotes else indexes will not be used.
Do not use the is null operator on a column that is indexed, as the optimizer will ignore the
index.
3. Object creation
Create the objects like tables and indexes in their respective tablespaces and also use proper
extent sizes and naming conventions.
Use B-tree indexes where possible as they are the best choice to ensure good performance for a
wide variety of queries.
In cost-based optimizer, there is no relevance to the order of the where clause predicates. Only if
the leading column of an index is referenced in the where clause will the index be used else not.
Whenever you have a series of columns used in the WHERE clause, it is always better to use a
COMPOSITE INDEX on all the columns used in the WHERE clause, instead of having a
separate index on each of the columns.
Split the indexes if it has more than 4 columns .The bigger the index the less efficient it gets and
the optimizer might decide against using it .However this will be an exception incase of a
covering index .
In case the usage of functions is unavoidable, use FUNCTION-BASED INDEXES on the
respective columns. If varchar2 column is indexed and is compared against a number, index is
discarded. Instead compare the column after converting the number to character using TO_CHAR
function for the number value.
4. Multi-Threading Processes
Can be used on any process which can be divided into multiple processes without having
to rewrite it.
Achieved by adding a condition in the query to do a range of records rather than all
Identify unnecessary full scans and remove them by adding indexes or query tuning.
Materialize your aggregations and summaries for static tables. Use proper formatting for
the SQL queries. This will avoid Hard Parsing at the database level.
Do not code correlated sub queries in applications, as they consume significant amounts of CPU
resources, use inline views instead. If a join will provide the functionality of the sub query try the
join method first.