Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using the SQL Access Advisor

Similar presentations


Presentation on theme: "Using the SQL Access Advisor"— Presentation transcript:

1 Using the SQL Access Advisor

2 Oracle Database 10g: Implement and Administer a Data Warehouse 13-2
Objectives After completing this lesson, you should be able to do the following: Use the SQL Access Advisor to optimize your entire workload Use the Enterprise Manager SQL Access Advisor Wizard Use the DBMS_ADVISOR procedures Oracle Database 10g: Implement and Administer a Data Warehouse

3 What Does the SQL Access Advisor Do?
Recommends the proper set of materialized views, materialized view logs, and indexes for a given workload Recommends bitmap, function-based, and B-tree indexes Recommends how to optimize materialized views so that they can be fast refreshable and take advantage of general query rewrite What Does the SQL Access Advisor Do? You can use the SQL Access Advisor to help you optimize your application. The SQL Access Advisor recommends the proper set of materialized views, materialized view logs, and indexes for a given workload. In addition, you can use the SQL Access Advisor to obtain recommendations on how to optimize materialized views so that they can be fast refreshable and how to take advantage of general query rewrite. Oracle Database 10g: Implement and Administer a Data Warehouse

4 SQL Access Advisor: Overview
What indexes and materialized views do I need to optimize my entire workload? SQL Access Advisor Solution DBA No expertise required Component of optimizer Workload SQL Access Advisor: Overview Defining appropriate access structures to optimize SQL queries has always been a concern for an Oracle DBA. As a result, there have been many papers and scripts written as well as high-end tools developed to address the matter. In addition, with the development of materialized view technology, deciding on access structures has become even more complex. SQL Access Advisor, as part of the manageability features of Oracle Database 10g, addresses this very critical need. The SQL Access Advisor identifies and helps resolve performance problems relating to the execution of SQL statements by recommending which indexes, materialized views, or materialized view logs to create, drop, or retain. It can be invoked from Enterprise Manager Database Control or from the command line by using PL/SQL procedures. Provides implementation script Oracle Database 10g: Implement and Administer a Data Warehouse

5 Oracle Database 10g: Implement and Administer a Data Warehouse 13-5
SQL Access Advisor: Overview (continued) The SQL Access Advisor takes an actual workload as input, or the Advisor can derive a hypothetical workload from the schema. It then recommends the access structures for a faster execution path. It provides the following advantages: Does not require you to have expert knowledge Bases decision making on rules that actually reside in the optimizer Is synchronized with the optimizer and Oracle database enhancements Is a single advisor covering all aspects of SQL access methods Provides simple, user-friendly GUI wizards Generates scripts for the implementation of recommendations Full Notes Page Oracle Database 10g: Implement and Administer a Data Warehouse

6 SQL Access Advisor: Usage Model
SQL cache User-defined Workload Hypothetical STS Filter options SQL Access Advisor: Usage Model The SQL Access Advisor takes as input a workload that can be derived from multiple sources: SQL cache, to take current content of V$SQL User-defined, to specify your workload in an input table Hypothetical, to generate a likely workload from your dimensional model SQL Tuning Sets (STSs), from the workload repository The SQL Access Advisor also provides powerful workload filters that you can use to target the tuning. For example, you can specify that the Advisor should look at only the 30 most resource-intensive statements in the workload, based on optimizer cost. For the given workload, the advisor then does the following: Simultaneously considers index solutions, materialized view solutions, or combinations of both Considers storage for creation and maintenance costs Does not generate drop recommendations for partial workloads Optimizes materialized views for maximum query rewrite usage and fast refresh Recommends materialized view logs for fast refresh Combines similar indexes into a single index Generates recommendations that support multiple workload queries Indexes Materialized views Materialized view logs Oracle Database 10g: Implement and Administer a Data Warehouse

7 Possible Recommendations
Comprehensive Limited Add new index on table or materialized view. YES Drop an unused index. NO Modify an existing index by changing the index type. Modify an existing index by adding columns at the end. Add a new materialized view. Drop an unused materialized view. Add a new materialized view log. Modify an existing materialized view log to add new columns or clauses. Possible Recommendations The SQL Access Advisor carefully considers the overall impact of recommendations and makes recommendations by using only the known workload and supplied information. Two workload analysis methods are available: Comprehensive: With this approach, the SQL Access Advisor addresses all aspects of tuning materialized views, indexes, and materialized view logs. It assumes that the workload contains a complete and representative set of application SQL statements. Limited: Unlike the comprehensive workload approach, a limited workload approach assumes that the workload contains only problematic SQL statements. Thus, advice is sought for improving the performance of a portion of an application environment. As shown in the table, the chosen workload approach determines the type of recommendations that the SQL Access Advisor makes. Note: When comprehensive workload analysis is chosen, the SQL Access Advisor forms a better set of global tuning adjustments, but the analysis time may be longer. Oracle Database 10g: Implement and Administer a Data Warehouse

8 Using the SQL Access Advisor
Invoke the SQL Access Advisor Wizard in Enterprise Manager. Use procedures in the DBMS_ADVISOR package. Using the SQL Access Advisor You can invoke the SQL Access Advisor from Oracle Enterprise Manager by using the SQL Access Advisor Wizard. If you choose not to use Enterprise Manager, you can use the SQL Access Advisor through the functions and procedures in the DBMS_ADVISOR package. Oracle Database 10g: Implement and Administer a Data Warehouse

9 SQL Access Advisor Repository
Contains all information needed and generated by the SQL Access Advisor Part of the database dictionary SQL Access Advisor Repository All information needed and generated by the SQL Access Advisor is held in the Advisor repository, which is a part of the database dictionary. The repository provides the following benefits: Collects a complete workload for the SQL Access Advisor Supports historical data Is managed by the server Oracle Database 10g: Implement and Administer a Data Warehouse

10 Privileges Needed to Use the SQL Access Advisor
ADVISOR system privilege SELECT privileges on tables targeted for materialized view analysis Privileges Needed to Use the SQL Access Advisor You need the ADVISOR privilege to manage or use the SQL Access Advisor. In addition, you must have SELECT privileges on the tables targeted for materialized view analysis. When processing a workload, the SQL Access Advisor attempts to validate each statement in order to identify table and column references. Validation is achieved by processing each statement as if it is being executed by the statement’s original user. If you do not have SELECT privileges for a particular table, the SQL Access Advisor bypasses the statement referencing the table causing the statement to be excluded from analysis. Note: The SELECT privileges cannot be obtained through a role. Oracle Database 10g: Implement and Administer a Data Warehouse

11 Invoking the SQL Access Advisor Wizard
You can access the SQL Access Advisor from the Advisor Central page of Enterprise Manager Database Control. Oracle Database 10g: Implement and Administer a Data Warehouse

12 Setting Initial Options
SQLAccessAdvOptions.gif Setting Initial Options When starting a SQL Access Advisor session, you can select Use Default Options and start with a predefined set of recommended Advisor options. Additionally, you can start a task and have it inherit a set of option values as defined by a template or task by selecting “Inherit Options from a Task or Template.” These include several generic templates designed for general purpose environments, OLTP, and data warehousing databases. You can save custom templates from a previous task and reuse them when needed. Click Continue to launch the wizard. Oracle Database 10g: Implement and Administer a Data Warehouse

13 Specifying the Workload Source
Use the SQL Access Advisor Wizard’s Workload Source page to provide a defined workload that allows the Access Advisor to make recommendations. Supported workload sources are: Current and Recent SQL Activity: Uses the current SQL from the cache Import Workload from SQL Repository: Enables you to specify a previously created SQL Tuning Set as the workload source User-Defined Workload; Import SQL from a Table or View: Enables you to receive recommendations for a workload that may not be running on the current database. This provides and implements access recommendations before an application goes live. Create a Hypothetical Workload from the Following Schemas and Tables: Provides a schema that allows the advisor to search for dimension tables and produce a workload The scope of the workload can be further reduced by applying filters that you can access in the Filter Options section. With these options, you can reduce the scope of the SQL statements that are present in the workload. The filters are applied to the workload by the advisor to focus the tuning effort. Possible filter options are: Top resource-consuming SQL statements Users, module identifier, or actions Tables Oracle Database 10g: Implement and Administer a Data Warehouse

14 Specifying Recommendation Options
Use the Recommendations Options page to choose whether to limit the advisor to recommendations based on a single access method. Choose Indexes or Materialized Views or both from the Recommendation Types section. You can choose Evaluation Only to evaluate only existing access structures. In this mode, the advisor does not generate new recommendations but comments on the use of existing structures. This is useful to track the effectiveness of the current index, materialized view, and materialized view log usage over time. You can use the Advisor Mode section to run the advisor in one of two modes. These modes affect the quality of recommendations as well as the length of time required for processing. In Comprehensive Mode, the advisor searches a large pool of candidates resulting in recommendations of the highest quality. In Limited Mode, the advisor performs quickly, limiting the candidate recommendations. Oracle Database 10g: Implement and Administer a Data Warehouse

15 Specifying Recommendation Options
Specifying Recommendation Options (continued) You can choose Advanced Options to show or hide options that enable you to set space restrictions, tuning options, and default storage locations. Use the Workload Categorization section to set options for Workload Volatility and Workload Scope. You can choose to favor read-only operations or you can consider the volatility of referenced objects when forming recommendations. You can also select Partial Workload, which does not include recommendations to drop unused access structures, or Complete Workload, which does include recommendations to drop unused access structures. Use the Space Restrictions section to specify a hard space limit, which forces the advisor to produce recommendations only with total space requirements that do not exceed the specified limit. Use the Tuning Options section to specify options that customize the recommendations made by the advisor. Use the “Prioritize tuning of SQL statements by” drop-down list to prioritize by Optimizer Cost, Buffer Gets, CPU Time, Disk Reads, Elapsed Time, and Execution Count. Oracle Database 10g: Implement and Administer a Data Warehouse

16 Oracle Database 10g: Implement and Administer a Data Warehouse 13-16
Specifying Recommendation Options (continued) Use the Default Storage Locations section to override the defaults defined for schema and tablespace locations. By default, indexes are placed in the schema and tablespace of the table they reference. Materialized views are placed in the schema and tablespace of the user who executed one of the queries that contributed to the materialized view recommendation. After you define these parameters, you can schedule and review your tuning task. Full Notes Page Oracle Database 10g: Implement and Administer a Data Warehouse

17 Oracle Database 10g: Implement and Administer a Data Warehouse 13-17
Viewing Results Viewing Results Using the Advisor Central page, you can list all the completed SQL Access Tuning tasks. Select the one for which you want to see the recommendations, and then click the View Result button. Use the Results for Task Summary page to get an overview of the advisor findings. The page presents charts and statistics that provide overall workload performance and query execution time potential improvement for the recommendations. You can use the page to show statement counts and recommendation action counts. To see other aspects of the results for the advisor task, click one of the three other tabs on the page: Recommendations, SQL Statements, or Details. Oracle Database 10g: Implement and Administer a Data Warehouse

18 Reviewing Recommendations
The Recommendations page displays a chart and a table that show the top recommendations ordered by their percentage improvement to the total cost of the entire workload. The top recommendations have the biggest total performance improvement. By clicking the Show SQL button, you can see the generated SQL script for the selected recommendations. You can click the corresponding recommendation identifier in the table to see the list of actions that need to be performed in order to implement the recommendation. On the Actions page, you can actually see all the corresponding SQL statements to execute in order to implement the action. For recommendations that you do not want to implement, keep those check boxes deselected. Then, click the Schedule Implementation button to implement the retained actions. This step is executed in the form of a Scheduler job. Oracle Database 10g: Implement and Administer a Data Warehouse

19 Using the DBMS_ADVISOR Package
1. Create a task and define parameters. 2. Define the workload. 3. Generate the recommendations. 4. View and implement the recommendations. Using the DBMS_ADVISOR Package You can use the procedures in the DBMS_ADVISOR package to generate recommendations from the SQL Access Advisor. Each step outlined in the slide will be described in detail in the next few pages. Oracle Database 10g: Implement and Administer a Data Warehouse

20 SQL Access Advisor Procedure Flow
Create/manage tasks and data. Prepare and analyze data. CREATE_TASK DELETE_TASK UPDATE_TASK… CREATE_SQLWKLD DELETE_SQLWKLD QUICK_TUNE Scripts ADD_SQLWKLD_REF DELETE_SQLWKLD_REF EXECUTE_TASK INTERRUPT/CANCEL_TASK MARK_RECOMMENDATION UPDATE_REC_ATTRIBUTES GET_TASK_REPORT SQLWkld object SQLAccess task Gather and manage workload. Prepare tasks. IMPORT_SQLWKLD… ADD_SQLWKLD_STAT… DELETE_SQLWKLD_STAT… UPDATE_SQLWKLD_STAT… SET_TASK_PARAMETER SET_SQLWKLD_PARAMETER RESET_TASK RESET_SQLWKLD SQL Access Advisor Procedure Flow The graphic shows the typical operational flow of the SQL Access Advisor procedures from the DBMS_ADVISOR package. Create and manage tasks and data using a SQL Workload object and SQL Access task. Prepare tasks for various operations using the workload and access parameters. Gather and manage the workload using a SQL Workload object. Prepare and analyze data using SQL Workload objects and SQL Access tasks. Note: The DBMS_ADVISOR.QUICK_TUNE procedure is a shortcut that performs all the necessary operations to analyze a single SQL statement. The operation creates a task for which all parameters are defaulted. The workload is constituted by the specified statement only. Finally, the task is executed and the results are saved in the repository. You can also instruct the procedure to implement the final recommendations. The DBMS_ADVISOR.QUICK_TUNE procedure is discussed later in the lesson. Oracle Database 10g: Implement and Administer a Data Warehouse

21 Oracle Database 10g: Implement and Administer a Data Warehouse 13-21
Creating a Task Define parameters for the task using DBMS_ADVISOR.SET_TASK_PARAMETER. Create a task using DBMS_ADVISOR.CREATE_TASK. VARIABLE task_id NUMBER; VARIABLE task_name VARCHAR2(255); EXECUTE :task_name := 'MYTASK'; EXECUTE DBMS_ADVISOR.CREATE_TASK ('SQL Access Advisor', :task_id, :task_name); Creating a Task You can control what a task does by defining parameters for that task using the DBMS_ADVISOR.SET_TASK_PARAMETER procedure. You create a task by using the CREATE_TASK procedure. In an Advisor task, you define what it is you want to analyze and where the results of this analysis should go. You can create any number of tasks, each with its own specialization. Oracle Database 10g: Implement and Administer a Data Warehouse

22 Setting SQL Access Advisor Parameters
Set parameters using the DBMS_ADVISOR procedures: SET_TASK_PARAMETER SET_SQLWKLD_PARAMETER Types of parameters: Workload filtering parameters Task configuration parameters Schema attributes parameters Setting SQL Access Advisor Parameters Use the SQL Access Advisor parameters to control the recommendation process and customization of the workload. These parameters enable you to control various aspects of the recommendation process, including the requested type of recommendation and the naming conventions for recommendations. You can also use parameters to control the workload existence and filtering that is to applied to the workload. Use the SET_TASK_PARAMETER and SET_SQLWKLD_PARAMETER procedures to set the SQL Access Advisor parameters. The parameter values persist for the life span of the task or workload object. Parameter values set using the SET_TASK_PARAMETER procedure do not change until you make another call to SET_TASK_PARAMETER. Oracle Database 10g: Implement and Administer a Data Warehouse

23 Oracle Database 10g: Implement and Administer a Data Warehouse 13-23
Using Templates Created by: Setting the template attribute when creating the task Using the UPDATE_TASK_ATTRIBUTE procedure Specified when using CREATE_TASK: VARIABLE template_id NUMBER; VARIABLE template_name VARCHAR2(255); EXECUTE :template_name := 'MY_TEMPLATE'; EXECUTE DBMS_ADVISOR.CREATE_TASK ('SQL Access Advisor',:template_id, - :template_name, is_template => 'TRUE'); Using Templates When an ideal configuration for a task or workload has been identified, you can save the configuration as a template upon which future tasks and workloads can be based. You can save time through the use of a template when performing tuning analysis by using it as a starting point for task creation. To create a task from a template, you specify the template to be used when a new task is created. At that time, the SQL Access Advisor copies the data and parameter settings from the template into the newly created task. You can also set an existing task to be a template by setting the template attribute when creating the task or by using the UPDATE_TASK_ATTRIBUTE procedure. Oracle Database 10g: Implement and Administer a Data Warehouse

24 Setting Template Parameters
Use DBMS_ADVISOR.SET_TASK_PARAMETER to set template parameters: EXECUTE DBMS_ADVISOR.SET_TASK_PARAMETER ( - :template_name,'INDEX_NAME_TEMPLATE', -'SH_IDX$$_<SEQ>'); :template_name, 'MVIEW_NAME_TEMPLATE', - 'SH_MV$$_<SEQ>'); Setting Template Parameters Before using the template to create a task, you can set template parameters. The following example illustrates how to set the naming conventions for recommended indexes and materialized views and the default tablespaces: Set naming conventions for recommended indexes and materialized views: EXECUTE DBMS_ADVISOR.SET_TASK_PARAMETER ( - :template_name, 'INDEX_NAME_TEMPLATE', 'SH_IDX$$_<SEQ>'); :template_name, 'MVIEW_NAME_TEMPLATE', 'SH_MV$$_<SEQ>'); Set the default tablespace for recommended indexes and materialized views: :template_name, 'DEF_INDEX_TABLESPACE', 'SH_INDEXES'); :template_name, 'DEF_MVIEW_TABLESPACE', 'SH_MVIEWS'); Oracle Database 10g: Implement and Administer a Data Warehouse

25 Using a Template to Create a Task
Specify the template when using DBMS_ADVISOR.CREATE_TASK: VARIABLE task_id NUMBER; VARIABLE task_name VARCHAR2(255); EXECUTE :task_name := 'MYTASK'; EXECUTE DBMS_ADVISOR.CREATE_TASK ('SQL Access Advisor', :task_id, - :task_name, template=>'MY_TEMPLATE'); Using a Template to Create a Task You can use your template as a starting point to create a task as shown in the slide. Oracle Database 10g: Implement and Administer a Data Warehouse

26 Oracle Database 10g: Implement and Administer a Data Warehouse 13-26
Defining the Workload Created using DBMS_ADVISOR.CREATE_SQLWKLD Linked to a task using DBMS_ADVISOR.ADD_SQLWKLD_REF Defining the Workload The workload consists of one or more SQL statements, plus various statistics and attributes that fully describe each statement. If the workload contains all SQL statements from a target business application, the workload is considered a full workload; if the workload contains a subset of SQL statements, it is known as a partial workload. Note that when you use a full workload, the SQL Access Advisor may recommend dropping certain existing materialized views and indexes if it finds that they are not being used effectively. The workload must contain a SQL query and the user who executed the query. All other attributes are optional. If the workload also contains I/O and CPU information, the SQL Access Advisor may be able to better evaluate the current efficiency of the statement. The workload is created using the DBMS_ADVISOR.CREATE_SQLWKLD procedure and is stored as a separate object enabling it to be shared among many Advisor tasks. After a workload has been created, it must be populated with information. The workload is then linked to a task using the DBMS_ADVISOR.ADD_SQLWKLD_REF procedure. Oracle Database 10g: Implement and Administer a Data Warehouse

27 Defining Workload Contents
SQL Tuning Sets User-defined workloads SQL cache workloads Hypothetical workloads Oracle Database 9i Summary Advisor workloads Defining the Workload (continued) After you have created the workload, you populate the workload. Ideally, a workload consists of the SQL statements that are being used against the database. The SQL Access Advisor can obtain its workload from the following sources: SQL Tuning Sets User-defined workloads SQL cache workloads Hypothetical workloads Oracle Database 9i Summary Advisor workloads Note: You can use the SQL Access Advisor without a workload; however, for best results, a workload should be provided. Oracle Database 10g: Implement and Administer a Data Warehouse

28 Importing a SQL Tuning Set
VARIABLE sqlsetname VARCHAR2(30); VARIABLE workload_name VARCHAR2(30); VARIABLE saved_stmts NUMBER; VARIABLE failed_stmts NUMBER; EXECUTE :sqlsetname := 'MY_STS_WORKLOAD'; EXECUTE :workload_name := 'MY_WORKLOAD'; EXECUTE DBMS_ADVISOR.CREATE_SQLWKLD (:workload_name); EXECUTE DBMS_ADVISOR.IMPORT_SQLWKLD_STS (:workload_name , - :sqlsetname, 'NEW', 1, :saved_stmts, :failed_stmts); Importing a SQL Tuning Set A SQL Tuning Set is the workload from the workload repository. You can use a SQL Tuning Set as the workload for the SQL Access Advisor by importing it using the IMPORT_WORKLOAD_STS procedure. After a workload has been collected and the statements filtered, the SQL Access Advisor computes usage statistics with respect to the DML statements in the workload. Note: A SQL Tuning Set (STS) is a database object that includes one or more SQL statements along with their execution statistics and execution context. Refer to the Oracle Database Performance Tuning Guide for detailed information about creating SQL Tuning Sets. Oracle Database 10g: Implement and Administer a Data Warehouse

29 Loading User-Defined Workloads
VARIABLE saved_stmts NUMBER; VARIABLE failed_stmts NUMBER; EXECUTE DBMS_ADVISOR.IMPORT_SQLWKLD_USER( - 'MYWORKLOAD', 'NEW', 'SH', 'USER_WORKLOAD', :saved_stmts, :failed_stmts); Loading User-Defined Workloads To load a user-defined workload, use the IMPORT_SQLWKLD_USER procedure. This procedure collects an application workload from a user-constructed table or view and saves it in the Advisor repository. The OWNER_NAME and TABLE_NAME parameters identify the source table for the workload. The format of the user table must correspond to the USER_WORKLOAD table and the user must have SELECT access to the workload table or view. Refer to the Oracle Database Performance Tuning Guide for a description of the USER_WORKLOAD table. Oracle Database 10g: Implement and Administer a Data Warehouse

30 Loading SQL Cache Workloads
VARIABLE saved_stmts NUMBER; VARIABLE failed_stmts NUMBER; EXECUTE DBMS_ADVISOR.IMPORT_SQLWKLD_SQLCACHE (- 'MYWORKLOAD', 'APPEND', 2, :saved_stmts, :failed_stmts); Loading SQL Cache Workloads You load a SQL cache workload by using the IMPORT_SQLWKLD_SQLCACHE procedure. When the procedure is called, the current contents of the SQL cache are analyzed and placed into the workload. Oracle Database 10g: Implement and Administer a Data Warehouse

31 Using Hypothetical Workloads
VARIABLE workload_name VARCHAR2(255); VARIABLE saved_stmts NUMBER; VARIABLE failed_stmts NUMBER; EXECUTE :workload_name := 'SCHEMA_WKLD'; EXECUTE DBMS_ADVISOR.CREATE_SQLWKLD(:workload_name); EXECUTE DBMS_ADVISOR.SET_SQLWKLD_PARAMETER (:workload_name, - VALID_TABLE_LIST, 'SH'); EXECUTE DBMS_ADVISOR.IMPORT_SQLWKLD_SCHEMA ( - :workload_name, 'NEW', 2, :saved_stmts, :failed_stmts); Using Hypothetical Workloads In many situations, an application workload may not yet exist. In this case, the SQL Access Advisor can examine the current logical schema design and form recommendations based on the relationships defined among tables. This type of workload is referred to as a hypothetical workload. The SQL Access Advisor can produce an initial set of recommendations and become a solid base for tuning an application. Execute the IMPORT_SQLWKLD_SCHEMA procedure to use a hypothetical workload. Note: To successfully import a hypothetical workload, the target schemas must contain dimension information. Oracle Database 10g: Implement and Administer a Data Warehouse

32 Adding SQL Statements to a Workload
VARIABLE sql_text VARCHAR2(400); EXECUTE :sql_text := 'SELECT AVG(amount_sold) FROM sales'; EXECUTE DBMS_ADVISOR.ADD_SQLWKLD_STATEMENT ( - 'MYWORKLOAD', 'MONTHLY', 'ROLLUP', priority=>1, executions=>10, - username => 'SH', sql_text => :sql_text); Adding SQL Statements to a Workload An alternative to importing a workload is to manually specify the SQL statements and add them to your workload by using the ADD_SQLWKLD_STATEMENT procedure. You can delete an existing SQL statement from a specified workload by using the DELETE_SQLWKLD_STATEMENT procedure. Use the SQL_ID to specify the statement to be deleted. You can modify SQL statements in a workload by using the UPDATE_SQLWKLD_STATEMENT procedure. Refer to the Oracle Database Performance Tuning Guide for additional information about deleting and modifying SQL statements in a workload. Oracle Database 10g: Implement and Administer a Data Warehouse

33 Oracle Database 10g: Implement and Administer a Data Warehouse 13-33
Removing Workloads EXECUTE DBMS_ADVISOR.DELETE_SQLWKLD ('MYWORKLOAD'); Removing Workloads You can remove a workload when it is no longer needed with the DELETE_SQLWKLD procedure. You can delete all workloads or a specific collection, but a workload cannot be deleted if it is still linked to a task. Oracle Database 10g: Implement and Administer a Data Warehouse

34 Linking the Workload to a Task
EXECUTE DBMS_ADVISOR.ADD_SQLWKLD_REF ('MYTASK', 'MYWORKLOAD'); Linking the Workload to a Task After you have defined the workload and populated it, you must link your task to the workload by using the ADD_SQKLWKLD_REF procedure. After a connection between an Advisor task and a workload is made, the workload is protected from removal or modification. Oracle Database 10g: Implement and Administer a Data Warehouse

35 Generating Recommendations
Generate recommendations by using DBMS_ADVISOR.EXECUTE_TASK. Recommendations are stored in the SQL Access Advisor Repository. Generating Recommendations After you have linked a workload to the task, generate recommendations by using the DBMS_ADVISOR.EXECUTE_TASK procedure. The recommendations are stored in the SQL Access Advisor Repository. The recommendation process generates several recommendations comprising one or more actions. For example, the recommendation may be to create a materialized view and then analyze it to gather statistical information. A task recommendation can range from a simple suggestion to a complex solution that requires implementation of a set of database objects such as indexes, materialized views, and materialized view logs. Oracle Database 10g: Implement and Administer a Data Warehouse

36 Viewing and Implementing Recommendations
Use catalog views: DBA/USER_ADVISOR_RECOMMENDATIONS Generate a script by using: DBMS_ADVISOR.GET_TASK_SCRIPT Viewing and Implementing Recommendations There are two ways to view the recommendations from the SQL Access Advisor: Use the catalog views DBA/USER_ADVISOR_RECOMMENDATIONS. Generate a script by using the DBMS_ADVISOR.GET_TASK_SCRIPT procedure. You do not have to accept all recommendations generated by the SQL Access Advisor. You can mark the ones that you want to include. The final step is implementing the recommendations and verifying that query performance has improved. Oracle Database 10g: Implement and Administer a Data Warehouse

37 SQL Access Advisor Actions
CREATE|DROP|RETAIN MATERIALIZED VIEW CREATE|ALTER|RETAIN MATERIALIZED VIEW LOG CREATE|DROP|RETAIN INDEX GATHER STATS SQL Access Advisor Actions Each recommendation consists of one or more actions, which must be implemented together to realize the benefit provided by the recommendation. The SQL Access Advisor produces the following types of actions: CREATE|DROP|RETAIN MATERIALIZED VIEW CREATE|ALTER|RETAIN MATERIALIZED VIEW LOG CREATE|DROP|RETAIN INDEX GATHER STATS The CREATE actions indicate that new objects should be created. The RETAIN recommendation indicates that existing objects should be kept. The GATHER STATS action generates a call to the DBMS_STATS procedure to gather statistics on a newly created object. Note: DROP recommendations are produced only if the WORKLOAD_SCOPE parameter is set to FULL. Oracle Database 10g: Implement and Administer a Data Warehouse

38 Generating SQL Scripts
Generate a CLOB containing the script for the recommendations: Save the Advisor script CLOB to a file: EXECUTE DBMS_ADVISOR.CREATE_FILE (DBMS_ADVISOR.GET_TASK_SCRIPT('MYTASK'), - 'ADVISOR_RESULTS', 'advscript.sql'); CREATE DIRECTORY ADVISOR_RESULTS AS '/mydir'; GRANT READ ON DIRECTORY ADVISOR_RESULTS TO PUBLIC; GRANT WRITE ON DIRECTORY ADVISOR_RESULTS TO PUBLIC; Generating SQL Scripts An alternative to querying the views to see the recommendations is to create a script of the SQL statements for the recommendations by using the GET_TASK_SCRIPT procedure. The resulting script is an executable SQL file that can contain DROP, CREATE, and ALTER statements. For new objects, the names of the materialized views, materialized view logs, and indexes are autogenerated by using the user-specified name template. The following parameters are used to define the objects: Object naming conventions: MVIEW_NAME_TEMPLATE and INDEX_NAME_TEMPLATE Object owner: DEF_INDEX_OWNER and DEF_MVIEW_OWNER Tablespace for object storage: DEF_MVIEW_TABLESPACE and DEF_INDEX_TABLESPACE You must generate a CLOB containing the script for the recommendations. To save the script to a file, a directory path must be specified to define where the CREATE_FILE procedure should store the script. In addition, read and write privileges must be granted on this directory. Oracle Database 10g: Implement and Administer a Data Warehouse

39 Performing a Quick Tune
Tune a single SQL statement by using the QUICK_TUNE procedure: VARIABLE task_name VARCHAR2(255); VARIABLE sql_stmt VARCHAR2(4000); EXECUTE :sql_stmt := 'SELECT COUNT(*) FROM customers WHERE cust_state_province=''CA'''; EXECUTE :task_name := 'MY_QUICKTUNE_TASK'; EXECUTE DBMS_ADVISOR.QUICK_TUNE (DBMS_ADVISOR.SQLACCESS_ADVISOR, - :task_name, :sql_stmt); Performing a Quick Tune If you want to tune only a single SQL statement, you can use the QUICK_TUNE procedure. It accepts a task name and SQL statement as input. It creates a task and workload and executes that task. The QUICK_TUNE procedure produces the same results as the EXECUTE_TASK procedure and is easier to use when there is only a single SQL statement to be tuned. Oracle Database 10g: Implement and Administer a Data Warehouse

40 Tuning Materialized Views for Fast Refresh and Query Rewrite
DBMS_MVIEW.EXPLAIN_MVIEW procedure: Advises whether a materialized view is fast refreshable or eligible for general query rewrite DBMS_MVIEW.EXPLAIN_REWRITE procedure: Advises whether query rewrite will occur DBMS_ADVISOR.TUNE_MVIEW procedure: Advises how to optimize your CREATE MATERIALIZED VIEW statement Tuning Materialized Views for Fast Refresh and Query Rewrite You can use the DBMS_MVIEW.EXPLAIN_MVIEW procedure to determine whether a materialized view is fast refreshable or eligible for general query rewrite. Use DBMS_MVIEW.EXPLAIN_REWRITE to determine whether query rewrite will occur. Note: The DBMS_MVIEW.EXPLAIN_MVIEW and the DBMS_MVIEW.EXPLAIN_REWRITE procedures are discussed in the lesson titled “Query Rewrite.” The DBMS_ADVISOR.TUNE_MVIEW procedure shows you how to optimize your CREATE MATERIALIZED VIEW statement. The TUNE_MVIEW procedure analyzes and processes the CREATE MATERIALIZED VIEW statement and generates two sets of output results: one for the materialized view implementation and the other for undoing the CREATE MATERIALIZED VIEW operations. The two sets of output results can be accessed through views or can be stored in external script files created by the SQL Access Advisor. The external script files can be executed to implement the materialized view. Oracle Database 10g: Implement and Administer a Data Warehouse

41 Using DBMS_ADVISOR.TUNE_MVIEW
TUNE_MVIEW takes two input parameters: TASK_NAME: A user-provided task identifier used to access the output results MV_CREATE_STMT: A complete CREATE MATERIALIZED VIEW statement DBMS_ADVISOR.TUNE_MVIEW ( task_name IN OUT VARCHAR2, mv_create_stmt IN [CLOB | VARCHAR2]) Using DBMS_ADVISOR.TUNE_MVIEW The TUNE_MVIEW procedure takes two input parameters: TASK_NAME: A user-provided task identifier used to access the output results MV_CREATE_STMT: A complete CREATE MATERIALIZED VIEW statement If the input CREATE MATERIALIZED VIEW statement does not have either the REFRESH FAST or the ENABLE QUERY REWRITE clause or neither of the two, TUNE_MVIEW uses the default REFRESH FORCE and DISABLE QUERY REWRITE clauses to tune the statement to be fast refreshable, if possible, or complete refreshable otherwise. The TUNE_MVIEW procedure can generate output statements that correct the defining query by adding extra columns or fix the materialized view logs so that FAST REFRESH is possible. In the case of a complex defining query, the TUNE_MVIEW procedure may decompose the query and generate two or more fast-refreshable materialized views or will restate the materialized view in a way to fulfill fast refresh requirements as much as possible. Oracle Database 10g: Implement and Administer a Data Warehouse

42 TUNE_MVIEW Output Results
Implementation: For implementing materialized views and required components Undo: For dropping materialized views and rewrite equivalences TUNE_MVIEW Output Results The TUNE_MVIEW procedure generates two sets of output results as executable statements as follows: Implementation: For implementing materialized views and required components such as materialized view logs or rewrite equivalences to achieve fast refreshability and query rewritablity as much as possible Undo: For dropping the materialized views and the rewrite equivalences in case you decide they are not required Oracle Database 10g: Implement and Administer a Data Warehouse

43 Implementation Process Output
CREATE MATERIALIZED VIEW LOG statements ALTER MATERIALIZED VIEW LOG FORCE statements One or more CREATE MATERIALIZED VIEW statements BUILD_SAFE_REWRITE_EQUIVALENCE statement Implementation Process Output The output statements for the implementation process include the following: CREATE MATERIALIZED VIEW LOG: Creates any missing materialized view logs required for fast refresh ALTER MATERIALIZED VIEW LOG FORCE: Fixes any materialized view log–related requirements, such as missing filter columns, sequence, and so on, required for fast refresh CREATE MATERIALIZED VIEW statements: In the case of one output statement, the original defining query is directly restated and transformed. A simple query transformation may be to add required columns. For example, add a ROWID column for a materialized join view and add an aggregate column for a materialized aggregate view. Multiple CREATE MATERIALIZED VIEW statements may be generated and form a nested materialized view hierarchy in which one or more submaterialized views are referenced by a new top-level materialized view modified from the original statement. This is to achieve fast refresh and query rewrite as much as possible. Submaterialized views are often fast refreshable. BUILD_SAFE_REWRITE_EQUIVALENCE statement: Enables the rewrite of top-level materialized views using submaterialized views. Oracle Database 10g: Implement and Administer a Data Warehouse

44 Oracle Database 10g: Implement and Administer a Data Warehouse 13-44
Undo Process Output DROP MATERIALIZED VIEW statements DROP_REWRITE_EQUIVALENCE statement Undo Process Output The output statements for the undo process include: DROP MATERIALIZED VIEW: To reverse the materialized view creations (including submaterialized views) in the implementation process DROP_REWRITE_EQUIVALENCE: To remove the rewrite equivalence relationship built in the implementation process if needed Note that the undo process does not include statements to drop materialized view logs. This is because materialized view logs can be shared by many different materialized views, some of which may reside on remote Oracle Database instances. Oracle Database 10g: Implement and Administer a Data Warehouse

45 Accessing TUNE_MVIEW Output Results
Script generation using the DBMS_ADVISOR.GET_TASK_SCRIPT function and the DBMS_ADVISOR.CREATE_FILE procedure USER_TUNE_MVIEW or DBA_TUNE_MVIEW views Accessing TUNE_MVIEW Output Results There are two ways to access TUNE_MVIEW output results: Script generation using the DBMS_ADVISOR.GET_TASK_SCRIPT function and the DBMS_ADVISOR.CREATE_FILE procedure USER_TUNE_MVIEW or DBA_TUNE_MVIEW views Oracle Database 10g: Implement and Administer a Data Warehouse

46 Oracle Database 10g: Implement and Administer a Data Warehouse 13-46
Summary In this lesson, you should have learned how to: Use the SQL Access Advisor to optimize your entire workload Use the Enterprise Manager SQL Access Advisor Wizard Use the DBMS_ADVISOR procedures Oracle Database 10g: Implement and Administer a Data Warehouse

47 Oracle Database 10g: Implement and Administer a Data Warehouse 13-47
Practice 13: Overview This practice covers using the SQL Access Advisor to tune a series of ad hoc queries. Oracle Database 10g: Implement and Administer a Data Warehouse

48


Download ppt "Using the SQL Access Advisor"

Similar presentations


Ads by Google