- Replace IN, NOT IN with Exists & Not Exists.
- Avoid using “Insert Into … Select …” statements as it can take lot time to execute if select query returns huge number of records. Instead user cursors.
- Avoid sub queries, instead use joins or subquery factoring (with clause) this will optimize the code.
- If insert & update statements are used inside Loops, then commit them after every 500 or 1000 updates or inserts & don’t forget to commit after end loop.
- Plan the deletion of records from the temporary / log tables created in the programs either using separate deletion program or in the same program where the tables are used.
- Analyze the tables with heavy updates to optimize the performance.
- Don’t forget to close the files & cursors opened in the program.
- Use ANSI SQL as they are easy to understand by any database developers and are highly optimizable.
- Fields used frequently in where clause of the queries should be indexed.
- Tables with records running over millions should be considered for partitioning.
- Avoid using string searches.
- Use type casting diligently. Stop writing queries that require implicit type casting as shown below.
Select *
from Employees
where EMPID = ‘100’;
EMPID is a numeric field, by enclosing 100 in single quotes we are triggering implicit type casting which may impact performance adversely.
- Avoid using OR by re-writing the query using UNION.
- Use Hints to optimize the queries that take too long to execute.
- Be cautious while using PARALLEL hint.
- Use Bulk Collect to optimize the PL/SQL code wherever possible.
- Too many indexes on a table may pull down the performance. Therefore, the number of indexes on a table should be between 5 to 7 only.
- Make use of function-based indexes, if function-based searches are used frequently.
- Never use * in select queries instead use fields names as SQL engine takes time in elaborating * with all fields name. Besides, if new field is added to the table, it is automatically added with the usage of *, which may not be required to be added resulting in run time error.
