I will explain Oracle SQL Tuning Tips and Tricks in this article.
Before SQL Tuning Tips and Tricks, you should read the following Performance Tuning tutorial, if you don’t know how to tune Oracle database Performance.
To increase all Database Performance, you should tune both database and SQL statements.
There are lots of tips and tricks for SQL Tuning but they may not work under every circumstance.
These tips and tricks are a reference while fixing performance problems in the Oracle Database.
1- Run TOP SQL Statements according to your Workload status.
You should know or find busy and free time of database.
Small transactions can run every time in the OLTP database, but Batch jobs or TOP SQL statements like ETL, Reporting and etc should not run every time.
If you run TOP SQL statements like ETL, Reporting and etc, then you should set available time of database for this task.
If your database load is low in the daylight, you can run it. if database is very busy, you may run it at night or weekend.
2- Make sure that CBO ( Cost based Optimizer ) statistics are correct
Statistic of Database and its objects should be up to date in Oracle database for Oracle optimizer. Because Oracle optimizer uses database statistics to generate lots of execution plans in same time and If statistics are up to date ,then Optimizer decide correct execution plans.
If your database statistics are staled ( out of date ), then you should update it. If you don’t know how to perform it, you can read the following article.
3- Check and Monitor Optimizer_Mode parameters
The default value of optimizer_mode parameter is ALL_ROWS which gives better throughput, but sometimes your application or software needs FIRST_ROWS if the response time is important.
Make sure that your software or application is running very well if the optimizer_mode is FIRST_ROWS.
3- Cursor Sharing and Bind Variable
You should set CURSOR_SHARING ( default value is Exact ) parameter value according to your database and software. If related application is not used Bind Variable , response time low due to a very high number of library cache misses and lots of SQL Statements matches with the text of SQL except the literal, then you should use CURSOR_SHARING=FORCE.
Actually Cursor_sharing= force is temporary solution, Permanent solution is to use Bind Variable.
Oracle doesn’t recommend CURSOR_SHARING=FORCE in the DSS(Decision support system) environment or if you are using complex queries
4- Index Usage
The usage of Index is very common especially in the performance tuning of Oracle Databases. It is one of the most important objects that are indispensable and proper performance tuning of databases.
You should use an index if less than 5% of the data needs to be accessed from a data set.
There are some advantages and disadvantages using Index in Oracle database. You should read the following article to read advantages and disadvantages of Index.
If your tables are getting extremely DML, you should not use so many Index on this table. Sometimes Dropping index will provide us extra performance.
If you don’t know how to create an Index, you should read the following article.
Use function-based indexes If queries contain a built-in function like to_char, decode, substr, etc. If you don’t use function-based indexes, SQL query may cause a full-table scan instead of Index scan.
If query contains WHERE lower(FIRST_NAME) statement, then index should be created as follows.
SELECT * FROM HR.EMPLOYEES WHERE lower(FIRST_NAME)='Donald';
CREATE INDEX EMPLOYEE_X2 ON HR.EMPLOYEES(LOWER(FIRST_NAME)) TABLESPACE HR_INDEX;
5- Materialized views usage
Materialized views is used to pre-sort sets, pre-summarize complex data warehouse information , pre-join tables and create complex objects conflicts dynamically with the demand for sub-second response time.
If you don’t use materialized views, you can see unnecessary repeating large-table full-table scans.
6- Remove Subqueries
You may remove subqueries (exists, in, not in) in the Complex queries and rewrite and use Join instead of subquery for faster performance.
7- Use Stored Procedure instead of Views
You should encapsulate the complex SQL inside a stored procedure instead of view usage. Because views are caused Unnecessary overhead and Excessive hard parsing problems.
8- Prefer uncomplicated SQL
it is not recommended to use complex SQL and subqueries. You should use WITH clause and Global temporary tables to divide and flatten-out queries.
Decomposing a query into multiple queries are better than a Complex query.
9- Use Union all instead of Union
If using union all is possible, you should prefer it instead of union, because Union are doing expensive sort to remove duplicate rows.
You should prefer uncomplicated ( Basic ) SQL instead of very complex SQL, If you cannot divide it, then You can make a very complex statement slightly less complex by using the UNION ALL operator
10- Avoid Having clause usage
Using Having clause is very expensive for the Performance tuning. You should prefer With clause instead of Having to decompose a complex query.
I will continue to explain tips and tricks of SQL Tuning in the next article.
Do you want to learn Oracle Database Performance Tuning detailed, then read the following articles.
916 views last month, 14 views today