13 copyright © 2005, oracle. all rights reserved. using the sql access advisor

45
13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

Upload: sophia-lamb

Post on 26-Mar-2015

223 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13Copyright © 2005, Oracle. All rights reserved.

Using the SQL Access Advisor

Page 2: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-2 Copyright © 2005, Oracle. All rights reserved.

Objectives

After completing this lesson, you should be able to do the following:

• Use the SQL Access Advisor to optimize yourentire workload

• Use the Enterprise Manager SQL Access Advisor Wizard

• Use the DBMS_ADVISOR procedures

Page 3: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-3 Copyright © 2005, Oracle. All rights reserved.

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

Page 4: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-4 Copyright © 2005, Oracle. All rights reserved.

SQL Access Advisor: Overview

Workload

SQL Access Advisor

Solution

Component of optimizer

Provides implementation

script

No expertiserequired

DBA

What indexes and materialized views

do I need to optimize my

entire workload?

Page 5: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-6 Copyright © 2005, Oracle. All rights reserved.

SQL Access Advisor: Usage Model

Indexes Materializedviews

Materializedview logs

SQL Access Advisor

User-defined

Hypothetical

SQL cache

Filter options

STS

Workload

Page 6: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-7 Copyright © 2005, Oracle. All rights reserved.

Possible Recommendations

Recommendation Comprehensive Limited

Add new index on table or materialized view. YES YES

Drop an unused index. YES NO

Modify an existing index by changing the index type.

YES NO

Modify an existing index by adding columns at the end.

YES YES

Add a new materialized view. YES YES

Drop an unused materialized view. YES NO

Add a new materialized view log. YES YES

Modify an existing materialized view log to add new columns or clauses.

YES YES

Page 7: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-8 Copyright © 2005, Oracle. All rights reserved.

Using the SQL Access Advisor

• Invoke the SQL Access Advisor Wizard in Enterprise Manager.

• Use procedures in the DBMS_ADVISOR package.

Page 8: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-9 Copyright © 2005, Oracle. All rights reserved.

SQL Access Advisor Repository

• Contains all information needed and generated by the SQL Access Advisor

• Part of the database dictionary

Page 9: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-10 Copyright © 2005, Oracle. All rights reserved.

Privileges Needed to Use the SQL Access Advisor

• ADVISOR system privilege

• SELECT privileges on tables targeted for materialized view analysis

Page 10: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-11 Copyright © 2005, Oracle. All rights reserved.

Invoking the SQL Access Advisor Wizard

Page 11: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-12 Copyright © 2005, Oracle. All rights reserved.

SQLAccessAdvOptions.gif

Setting Initial Options

Page 12: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-13 Copyright © 2005, Oracle. All rights reserved.

Specifying the Workload Source

Page 13: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-14 Copyright © 2005, Oracle. All rights reserved.

Specifying Recommendation Options

Page 14: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-15 Copyright © 2005, Oracle. All rights reserved.

Specifying Recommendation Options

Page 15: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-17 Copyright © 2005, Oracle. All rights reserved.

Viewing Results

Page 16: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-18 Copyright © 2005, Oracle. All rights reserved.

Reviewing Recommendations

Page 17: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-19 Copyright © 2005, Oracle. All rights reserved.

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.

Page 18: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-20 Copyright © 2005, Oracle. All rights reserved.

SQL Access Advisor Procedure Flow

SQLWkldobject

SQLAccesstask

CREATE_TASKDELETE_TASKUPDATE_TASK…CREATE_SQLWKLDDELETE_SQLWKLDQUICK_TUNE

SET_TASK_PARAMETERSET_SQLWKLD_PARAMETERRESET_TASKRESET_SQLWKLD

IMPORT_SQLWKLD…ADD_SQLWKLD_STAT…DELETE_SQLWKLD_STAT…UPDATE_SQLWKLD_STAT…

ADD_SQLWKLD_REFDELETE_SQLWKLD_REFEXECUTE_TASKINTERRUPT/CANCEL_TASKMARK_RECOMMENDATIONUPDATE_REC_ATTRIBUTES GET_TASK_REPORT

Create/manage tasks and data.

Gather and manage workload.Prepare tasks.

Prepare and analyze data.Scripts

Page 19: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-21 Copyright © 2005, Oracle. All rights reserved.

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);

Page 20: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-22 Copyright © 2005, Oracle. All rights reserved.

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

Page 21: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-23 Copyright © 2005, Oracle. All rights reserved.

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');

Page 22: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-24 Copyright © 2005, Oracle. All rights reserved.

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>');EXECUTE DBMS_ADVISOR.SET_TASK_PARAMETER ( -:template_name, 'MVIEW_NAME_TEMPLATE', -

'SH_MV$$_<SEQ>');

Page 23: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-25 Copyright © 2005, Oracle. All rights reserved.

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');

Page 24: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-26 Copyright © 2005, Oracle. All rights reserved.

Defining the Workload

• Created using DBMS_ADVISOR.CREATE_SQLWKLD

• Linked to a task using DBMS_ADVISOR.ADD_SQLWKLD_REF

Page 25: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-27 Copyright © 2005, Oracle. All rights reserved.

Defining Workload Contents

• SQL Tuning Sets

• User-defined workloads

• SQL cache workloads

• Hypothetical workloads

• Oracle Database 9i Summary Advisor workloads

Page 26: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-28 Copyright © 2005, Oracle. All rights reserved.

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);

Page 27: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-29 Copyright © 2005, Oracle. All rights reserved.

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);

Page 28: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-30 Copyright © 2005, Oracle. All rights reserved.

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);

Page 29: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-31 Copyright © 2005, Oracle. All rights reserved.

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);

Page 30: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-32 Copyright © 2005, Oracle. All rights reserved.

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);

Page 31: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-33 Copyright © 2005, Oracle. All rights reserved.

Removing Workloads

EXECUTE DBMS_ADVISOR.DELETE_SQLWKLD ('MYWORKLOAD');

Page 32: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-34 Copyright © 2005, Oracle. All rights reserved.

Linking the Workload to a Task

EXECUTE DBMS_ADVISOR.ADD_SQLWKLD_REF('MYTASK', 'MYWORKLOAD');

Page 33: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-35 Copyright © 2005, Oracle. All rights reserved.

Generating Recommendations

• Generate recommendations by using DBMS_ADVISOR.EXECUTE_TASK.

• Recommendations are stored in the SQL Access Advisor Repository.

Page 34: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-36 Copyright © 2005, Oracle. All rights reserved.

Viewing and Implementing Recommendations

• Use catalog views: DBA/USER_ADVISOR_RECOMMENDATIONS

• Generate a script by using: DBMS_ADVISOR.GET_TASK_SCRIPT

Page 35: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-37 Copyright © 2005, Oracle. All rights reserved.

SQL Access Advisor Actions

• CREATE|DROP|RETAIN MATERIALIZED VIEW• CREATE|ALTER|RETAIN MATERIALIZED VIEW LOG• CREATE|DROP|RETAIN INDEX• GATHER STATS

Page 36: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-38 Copyright © 2005, Oracle. All rights reserved.

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;

Page 37: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-39 Copyright © 2005, Oracle. All rights reserved.

Performing a Quick Tune

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);

Tune a single SQL statement by using the QUICK_TUNE procedure:

Page 38: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-40 Copyright © 2005, Oracle. All rights reserved.

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

Page 39: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-41 Copyright © 2005, Oracle. All rights reserved.

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])

Page 40: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-42 Copyright © 2005, Oracle. All rights reserved.

TUNE_MVIEW Output Results

• Implementation: For implementing materialized views and required components

• Undo: For dropping materialized views and rewrite equivalences

Page 41: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-43 Copyright © 2005, Oracle. All rights reserved.

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

Page 42: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-44 Copyright © 2005, Oracle. All rights reserved.

Undo Process Output

• DROP MATERIALIZED VIEW statements

• DROP_REWRITE_EQUIVALENCE statement

Page 43: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-45 Copyright © 2005, Oracle. All rights reserved.

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

Page 44: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-46 Copyright © 2005, Oracle. All rights reserved.

Summary

In this lesson, you should have learned how to:

• Use the SQL Access Advisor to optimize yourentire workload

• Use the Enterprise Manager SQL Access Advisor Wizard

• Use the DBMS_ADVISOR procedures

Page 45: 13 Copyright © 2005, Oracle. All rights reserved. Using the SQL Access Advisor

13-47 Copyright © 2005, Oracle. All rights reserved.

Practice 13: Overview

This practice covers using the SQL Access Advisor to tune a series of ad hoc queries.