Oracle Database 10g comes bundled with a number of advisors, a few of which you have already seen in previous chapters. This chapter discusses the SQL advisorsnamely, the SQL Tuning Advisor and the SQL Access Advisor. We have combined the coverage of both these advisors into one chapter because they are somewhat related and operate exclusively on SQL statements. Before launching into the details, it is wise to have some background information about these advisors as well as about SQL tuning in general so that you can keep everything in perspective. The SQL Tuning AdvisorThe SQL Tuning Advisor is actually a front-end interface to the deeper Automatic SQL Tuning capability of the query optimizer. The main objective of the SQL Tuning Advisor (STA in its short form) is to automate the entire process of tuning SQL. Along with the SQL Access Advisor, it tries to automate one of the hardest and most complex tasks of a performance analystthat of changing the SQL or the environment in which it works so that the SQL statement runs more efficiently. The query optimizer actually operates in two modes: normal and tuning. In normal mode, the optimizer parses and executes a SQL without spending too much time generating the execution plan, which may hence be suboptimal. In tuning mode, the optimizer performs additional analysis to check whether the execution plan previously produced under the normal mode can be further improved. In this mode, however, the query optimizer does not execute the SQL, but similarly to ADDM produces a series of recommendations along with their rationale and the expected benefit for producing a significantly superior plan. When called under tuning mode, the optimizer is referred to as the Automatic Tuning Optimizer and the tuning performed therein is called Automatic SQL Tuning. The SQL Tuning Advisor simply exposes these recommendations and provides an interface to perform such tuning. All this is again possible via the use of an advisor framework in which advisor specific tasks are created and submitted to the advisors for processing.
Automatic SQL TuningWe mentioned the two modes in which the query optimizer executes. Let's now look in detail at the tuning mode, because this is the mode that is involved with the SQL Tuning Advisor. This kernel code component is also known as the Automatic Tuning Optimizer, and is an integral part of the query optimizer. This integration provides several advantages:
In fact, you can consider the Automatic Tuning Optimizer as an extension and improvement to the runtime query optimizer that is available on call when required. This way, the overhead associated with in-depth analysis is not imposed on the normal, everyday SQL statements during parsing, but is reserved for invocation on high-load SQLs only, aiding efficient use of computing resources. In normal mode, during the parse phase, the query optimizer generates an execution plan for a given SQL statement within a short amount of time, usually less than a second and at worst no more than few seconds. Because of this self-imposed stringent requirement, the optimizer performs a limited plan search by using built-in heuristics to pare down the optimization phase of parsing. As well, there is no investigation and verification of objects and their statistics during the plan-generation process. On the other hand, the Automatic Tuning Optimizer is typically given much more time, usually in minutes, to perform the necessary investigation and verification steps as part of the tuning process. Thus, the Automatic Tuning Optimizer has a much higher probability of generating a well-tuned plan. Because it is given sufficient time, the Automatic Tuning Optimizer uses dynamic sampling and partial execution techniques (that is, execution of fragments of a SQL statement) to verify its own estimates of cost, selectivity, and cardinality. It also uses the past execution history of the SQL statement to determine optimal settings when such history is available. For example, if it determines that a small number of rows will be returned by the SQL, then it may switch the OPTIMIZER_MODE from the default ALL_ROWS (which works toward higher throughput) to FIRST_ROWS (which works toward higher initial response).
When invoked, the Automatic Tuning Optimizer performs four types of analyses during the plan-generation phase:
As you may have noticed, these steps are similar in both sequence and nature to those used during manual SQL tuning. Automatic SQL tuning just performs this for you on demand, tirelessly, automatically, and free of charge! The SQL Tuning Advisor makes this wealth of tuning information available in an understandable and usable form. The SQL Tuning Advisor is available for access both via OEM as well as via the advisor and SQL tuning API, described later in this chapter. Inputs to the SQL Tuning AdvisorThe SQL Tuning Advisor takes inputs from multiple sources. The SQL Tuning Advisor can deal with more than one SQL statement at a time, so you can submit a composite SQL workload for analysis. As you saw in previous chapters, high-load SQL statements identified by the ADDM form the main input for the SQL Tuning Advisor, which now plays the role of the Specialist in the Patient-General Practitioner-Specialist story. Equally well, these statements could have also been identified by the AWR or even by a manual process. The manual process could include the testing of a set of SQL statements that are yet to be deployed, tuning individual performance as well as a collection of SQL statements. This collection is now turned into a persistent tuning object called the SQL tuning set (STS), which is described in detail later in this chapter. The SQL Access AdvisorThe SQL Access Advisor works alongside the SQL Tuning Advisor and could be called by the former when appropriate. It is a tuning tool that provides advice specifically on materialized views, indexes, and materialized view logs. Given a specified workload in the form of a SQL tuning set or even individual SQL statements, the SQL Access Advisor recommends the creation of materialized views, materialized view logs, and indexes for a given workload. In general, as the number of materialized views and indexes and the space allocated to them is increased, query performance improves. The SQL Access Advisor considers the tradeoffs between space usage and query performance and recommends the most cost-effective configuration of new and existing materialized views and indexes. The SQL Access Advisor is actually based on and builds upon the Oracle 9i Summary Advisor. The SQL Access Advisor interfaces to generate SQL when requested. You can get to this tool both via the OEM as well as via the advisor and SQL tuning API.
Inputs to the SQL Access AdvisorThe SQL Access Advisor can use the following inputs in order to operate on and produce recommendations:
The steps to use the SQL Access Advisor are as follows:
As with all other advisors, the option is left to the performance analyst or DBA to implement the recommendations. |