From:       To:      
Home > Documentation > Oracle

Tuning SQL Queries

Effective optimization of SQL queries must start from overall optimization of Oracle system (if it has not been done before). Otherwise, it may force you to rewrite tuned execution plans later. You can find some hints on system-level Oracle tuning in this article:

https://www.convert-in.com/docs/oracle/system_optimization.htm

Oracle SQL tuning is a very complicated procedure having primary goals to improve the execution plan to fetch the rows with the minimal number of reads from the database. Digging deeper into details, those goals can be reached through the following steps:

Oracle SQL Optimizer

The optimizer determines the best way to execute a SQL query based on the statement structure, statistical information about the involved database objects and all the relevant execution features.

The Oracle optimizer must make three major decisions during the optimization of a SQL statement: method to access the data, join method (nested loops, hash joins, etc) and tables join order.

The cost-based optimizer relies on statistics gathered from the table through the 'analyze table' command. These statistics provide Oracle with valuable information about the tables, enabling it to make intelligent decisions about the most efficient approach to handle SQL queries. However, it's important to understand that the cost-based optimizer may not always choose the optimal path for query speed. Although ongoing enhancements are being made to the cost-based optimizer, there are still situations where the rule-based optimizer can produce faster Oracle queries.

By default, Oracle optimizer uses 'all_rows' mode that is tailored to minimize usage of computational resource and dur to this fact it prioritizes full-table scans over index access. On the other hand, index access ('first_rows_n' optimizer mode) may cause extra I/O overhead, but it allows the originating query to return rows faster.

If not all tables involved in the query contain CBO statistics, Oracle estimates statistics at runtime that may essentially decrease performance of the query.

Since SQL statements typically fetch only a small subset of rows from a table, Oracle optimizers are designed to identify required indexes and use to reduce excessive I/O whenever it is possible. Nevertheless, if a query's statement is inefficient, the cost-based optimizer may become uncertain about the optimal data access path and occasionally choose a full-table scan. Consequently, the best practice for Oracle database administrators to investigate SQL statements and actively search for opportunities to avoid full-table scans.

Oracle SQL Tuning Plan

Step 1. Identify high-impact SQL. All SQL statements must be sorted number of executions descending and tuned in this order. The frequency of query usage can be located in stats$sql_summary.EXECUTIONS. Table stats$sql_summary and view v$sqlarea contain the following important information about SQL statements:

Step 2. Determine the execution plan. After each SQL statement is identified, next step is to 'explain' it in order to determine the execution plan. A lot of third-party tools can show the execution plan for SQL statements. The most straightforward option to determine the execution plan for SQL query is using explain plan utility provided by Oracle. It allows Oracle DBA to parse the statement and explore the execution class path without actually executing the query.

The result is arranged as a special 'plan' table containing the following fields:

Although plan table helps to determine the access path to the data, this information may be not enough. The SQL optimizer knows number of rows and presence of indexes on fields for each table, but it is not aware about important factors of data distribution (for example, the number of rows that are expected from each query component).

Step 3. Tune the SQL statement. For SQL statements with less-than-optimal execution plans, tuning can be performed through one of the following methods: