Oracle provides several methods for reducing the time spent parsing Oracle SQL statements, which can cause a drag on performance when executing complex queries with a large number of possible execution plans. Let’s briefly examine some of these methods.
Hinting around with the ordered hint
Oracle must spend a great deal of time parsing multiple table joins to determine the optimal order to join the tables. SQL statements with table joins involving seven or more tables can sometimes take more than 30 minutes to parse because Oracle must evaluate all possible table join orders. This can add up to more than 40,000 orders with only eight tables. The ordered hint is commonly used in conjunction with other hints to suggest a proper join order.
The ordered hint requests that the tables listed in the FROM clause of a SQL statement be joined in the order specified, with the first table in the FROM clause specifying the driving table. The driving table should be the table that returns the smallest number of rows. Using the ordered hint will bypass the very expensive and time-consuming parsing operation and speed the execution of Oracle SQL.
In Listing A is an example of a complex query that is forced to perform a nested loop join with a parallel query on the emp table. Note that I’ve used the ordered hint to direct Oracle to evaluate the tables in the order they are presented in the WHERE clause.
Using theordered_predicates hint
The ordered_predicates hint is specified in the WHERE clause of a query and is used to specify the order in which Boolean predicates should be evaluated. In the absence of ordered_predicates, Oracle uses the following steps to evaluate the order of SQL predicates:
- Subqueries are evaluated before the outer Boolean conditions in the WHERE clause.
- All Boolean conditions without built-in functions or subqueries are evaluated in reverse from the order they are found in the WHERE clause, with the last predicate being evaluated first.
- Boolean predicates with built-in functions of each predicate are evaluated in increasing order of their estimated evaluation costs.
You can override these default evaluation rules using the ordered_predicates hint, and your WHERE clause items will then be evaluated in the order that they appear in the query. The ordered_predicates hint is commonly used in cases where a PL/SQL function is used in the WHERE clause of a query. It is also very useful in cases where you know the most restrictive predicates and you want Oracle to evaluate these first.
You cannot use the ordered_predicates hint to preserve the order of predicate evaluation on index keys.
Limit the number of table join evaluations
One final way you can improve the performance of SQL parsing is to override Oracle’s parameter that controls the number of possible joins evaluated by the cost-based optimizer when evaluating a query. The optimizer_search_limit parameter specifies the maximum number of table join combinations that will be evaluated when Oracle tries to decide the best way to join multiple tables. This parameter helps prevent the optimizer from spending more time evaluating every possible join ordering than could be saved by finding the optimal join order. The optimizer_search_limit parameter also controls the threshold for invoking a star join hint, and a star hint will be honored when the number of tables in the query is less than the optimizer_search_limit. The default value is 5.
More on optimizer parameters
For more on tweaking Oracle’s optimizer for maximum performance with complex queries, see “Boost Oracle performance with intelligent optimizer limits.”
These are just a few of the tricks that Oracle DBAs use to optimize the performance of SQL queries in an Oracle database application.