Below are some of the basic hygiene techniques and concepts we should keep in mind to get the maximum performance out of our query .
- We should always add adequate predicates to our query. We should always use index columns in our query. And also try to use the index columns in our predicates.
- If possible, select only the index columns in the SQL query.
- We should always keep in mind the selectivity of our query. If the selectivity of a table is more than 5%, then there is no certainty if the optimizer would choose the index or not.
- We should never perform or use arithmetic operations on the indexed columns.
- We should avoid using the like character in the beginning like ‘%on’. Instead we should use the reverse index. Eg where reverse(last_name) like ‘on%’.
- Do not use functions on the indexed columns instead we should always use a query .
- B-Tree index do not use indexed the null values and this sometimes the suppress the index usage in our queries. To avoid this we can use IS NOT NULL in where clause .Its recommended to use BITMAP Index instead of B-Tree index.
- We should use Truncate instead of Delete (Truncate does not generate redo). Truncate makes unusable index usable again.
- We should always check the datatypes of the columns and compare values because if they don’t match the optimizer my skip the index usage.
- We should avoid using the having by clause because it only restricts or eliminates the rows after they are read from the memory or disc.
- If you don’t need to use all the table in a view, we should avoid using this view in our queries.
- We should always use materialized views, partition table .
- We should create Partition table for queries having low selectivity.
- Its recommended to use bulk collect as it reduces the context switching. Implicit cursor use bulk collect by default by fetching first 100 rows .
- Using Bind variables: Bind variables are used when we execute the same query again and again but only the predicate value change. It increase the performance by decreasing the parse count .
- Bind Variable Peeking : Bind variable peeking is a process when the optimizer waits/peeks for the first variable before deciding on the execution plan . It will generate the plan only after getting the first bind variable.
- We should use bind variable only when the cardinality of the values in the column is similar .
- We should never use the bind variable if the selectivity of each variable is pretty different .
- After 12c adaptive cursor sharing is used by default.
- Adaptive cursor sharing automatically detects is the query needs another execution plan or can use the existing plan by using bind aware and bind sensitive cursor .