ORACLE SQL DEVELOPER 优化SQL语句

sql Tuning Advisor in Oracle sql Developer 3.2

Overview

    Purpose

    This tutorial shows you how to use the sql Tuning Advisor feature in Oracle sql Developer 3.2.

    Time to Complete

    Approximately 40 minutes

    Introduction

    The sql Tuning Advisor analyzes high-volume sql statements and offers tuning recommendations. It takes one or more sql statements as an input and invokes the Automatic Tuning Optimizer to perform sql tuning on the statements. It can run against any given sql statement. The sql Tuning Advisor provides advice in the form of precise sql actions for tuning the sql statements along with their expected performance benefits. The recommendation or advice provided relates to the collection of statistics on objects,creation of new indexes,restructuring of the sql statement,or creation of a sql profile. You can choose to accept the recommendation to complete the tuning of the sql statements.

    Oracle Database can automatically tune sql statements by identifying problematic sql statements and implementing tuning recommendations using the sql Tuning Advisor. You can also run the sql Tuning Advisor selectively on a single or a set of sql statements that have been identified as problematic.

    In this tutorial,you learn how to run and review the recommendations of the sql Tuning Advisor.

    Note: Tuning Advisor is part of the Tuning Pack,one of the Oracle management packs and is available for purchase with Enterprise Edition. For more information see The Oracle Technology Network or theonline documentation.

    Hardware and Software Requirements

    The following is a list of hardware and software requirements:

    • Oracle Database 11g Enterprise Edition with access to the Tuning and Diagnostic management packs and with the sample schema installed.
    • Oracle sql Developer 3.2.

    Prerequisites

    • Download Oracle sql Developer 3.2 here.

    Note: For best results,use Firefox or Chrome browsers to view this tutorial.

Creating a Database Connection

    The first step to managing database objects using Oracle sql Developer 3.2 is to create a database connection.

    Perform the following steps to create a database connection:

    Note: If you already have database connections for HR and SYSTEM,you do not need to perform the following steps. You can move toProviding Privileges to the Hr Usertopic.

    If you have installed the sql Developer icon on your desktop,click the icon to start your sql Developer and move to Step 4. If you do not have the icon located on your desktop,perform the following steps to create a shortcut to launch sql Developer 3.2 from your desktop.

    Open the directory where the sql Developer 3.2 is located,right-click sqldeveloper.exe (on Windows) or sqldeveloper.sh (on Linux) and select Send to > Desktop (create shortcut).

    On the desktop,you will find an icon named Shortcut to sqldeveloper.exe. Double-click the icon to open sql Developer 3.2.

    Note: To rename it,select the icon and then press F2 and enter a new name.


    Your Oracle sql Developer opens up.

    In the Connections navigator,right-click Connections and select New Connection.

    The New / Select Database Connection dialog opens. Enter the connection details as follows and clickTest.

    Connection Name: system

    Username: system

    Password: <your_password > (Select Save Password)

    Hostname: localhost

    SID: <your_own_SID>


    Check for the status of the connection on the left-bottom side(above the Help button). It should readSuccess. ClickSave and then click Connect.

    In the Connections navigator,to create a new connection to the hr schema,right-clickConnections and selectNew Connection.

    The New / Select Database Connection dialog opens. Enter the connection details as follows and clickTest.

    Connection Name: hr

    Username: hr

    Password: <your_password > (Select Save Password)

    Hostname: localhost

    SID: <your_own_SID>

    Check for the status of the connection on the left-bottom side(above the Help button). It should readSuccess. ClickSave and then click Connect.

Providing Privileges and Removing the Existing Statistics on the HR User

    A user requires certain privileges to run the sql Tuning Advisor. Also,in order to collect and manage statistics on the HR schema,the existing statistics need to be cleared. Below are the steps to grant sql Tuning Advisor privileges and to remove the existing statistics on the hr user.

    Click sql Worksheet and select system user.


    To grant privileges to the hr user to run the sql Tuning Advisor,enter the following lines of code. Click Run Script.
    grant advisor to hr;
    grant administer sql tuning set to hr;


    The output for the statements is displayed.


    The Oracle database allows you to collect statistics of many different kinds in order to improve performance. To illustrate some of the features the sql Tuning Advisor offers,clear the existing statistics from the HR schema.

    To delete the schema statistics,enter the following line of code.

    exec DBMS_STATS.DELETE_SCHEMA_STATS ('hr');

    Select the statement and click Run Statement

    With the DBMS_STATS package you can view and modify optimizer statistics gathered for database objects.TheDELETE_SCHEMA_STATS procedure deletes statistics for an entire schema.


    The outputs for the statements are displayed.


Running the sql Tuning Advisor on a sql statement

    In this topic,you run the sql Tuning Advisor on a sql statement. Four types of analysis are performed by the sql Tuning Advisor on the sql statement.
    All the recommendations are displayed in the Overview. You can also view each recommendation individually.

    Open the sql Worksheet for the hr user by clicking sql Worksheet.


    Enter the following sql statement in the worksheet.
    select sum(e.salary),avg(e.salary),count(1),e.department_id from departments d,employees e group by e.department_id order by e.department_id;

    Select the sql statement and click sql Tuning Advisor .


    The sql Tuning Advisor output appears.


    In the left navigator,click Statistics. In this analysis,objects with stale or missing statistics are identified and appropriate recommendations are made to remedy the problem.


    In the left navigator,click sql Profile. Here,the sql Tuning Advisor recommends to improve the execution plan by the generation of a sql Profile.


    Click the Detail tabbed page to view the sql Profile Finding.


    In the left navigator,click Indexes. This recommends whether the sql statement might benefit from an index. If necessary,new indexes that can significantly enhance query performances are identified and recommended.


    Click the Overview tabbed page. In this case,there are no index recommendations.


    In the left navigator,click Restructure sql. In this analysis,relevant suggestions are made the restructure selected sql statements for improved performance.



Implementing sql Tuning Advisor Recommendations

    You can implement the sql Tuning Advisor recommendation feature. This will enable you to update the statistics in hr schema. Perform the following steps to implement the sql Tuning Advisor recommendations:

    In the Connections navigator,right-click hr and select Gather Schema Statistics....


    In Gather Schema Statistics,select Estimate Percent as100 from the drop-down list so that all rows in each table are read. This ensures that the statistics are as accurate as possible.


    Click Apply.


    A confirmation message appears. Click OK.


    To run the sql Tuning Advisor on the sql statement again,select the sql statement and click sql Tuning Advisor.


    The sql Tuning Advisor output appears. By gathering statistics,the Statistics and sql Profile advice is now removed.


    In the left navigator,click each of the sql Tuning Advisor Implement Type to check if all the recommendations have been implemented.


    Note the issues reported to you:

    Note the issues reported to you:

    Note that the Restructure sql recommendation to remove an unused table remains.

    Remove the "departments" table in the sql statement and click sql Advisor.

    select sum(e.salary),e.department_id from employees e
    group by e.department_id order by e.department_id;


    The output appears. All of the advice recommendations have been removed.



Summary


    In this tutorial,you have learned how to:

    • Create a Database Connection
    • Provide Privileges to the HR User
    • Run the sql Tuning Advisor on a sql statement
    • Implement sql Tuning Advisor Recommendations

    Credits

    • Lead Curriculum Developer: Dimpi Sarmah,Sharon Stephen
    • Other Contributors: Ashley Chen,Swarnapriya Shridhar,Nancy Greenberg

相关文章

数据库版本:11.2.0.4 RAC(1)问题现象从EM里面可以看到,在23号早上8:45~8:55时,数据库等待会话暴增...
(一)问题背景最近在对一个大约200万行数据的表查看执行计划时,发现存在异常,理论上应该返回100多万...
(一)删除备份--DELETE命令用于删除RMAN备份记录及相应的物理文件。当使用RMAN执行备份操作时,会在RM...
(1)DRA介绍 数据恢复顾问(Data Recovery Advise)是一个诊断和修复数据库的工具,DRA能够修复数据文...
RMAN(Recovery Manager)是Oracle恢复管理器的简称,是集数据库备份(backup)、修复(restore)和恢复...
(1)备份对象 可以使用RMAN进行的备份对象如下: --整个数据库:备份所有的数据文件和控制文件; --数...