学导网 >> it000 >> Oracle
如何分析SQL语句
www.xuedao.net  2007-6-12 14:37:00 网络

   很多时候,我们不太清楚自己写的SQL语句好还是不好,往往数据量一大,程序运行变慢。其实在SQL/PLUS里可以很清晰的分析出SQL语句的执行计划,它可以提醒我们来创建索引或改变SQL语句的写法。
    先在sys用户下运行@/ORACLE_HOME/sqlplus/admin/plustrce.sql

内容:
set echo on
drop role plustrace;
create role plustrace;
grant select on v_$sesstat to plustrace;
grant select on v_$statname to plustrace;
grant select on v_$session to plustrace;
grant plustrace to dba with admin option;
set echo off

产生plustrace角色,然后在sys用户下把此角色赋予一般用户&username

SQL> grant plustrace to &username;
    然后找到/ORACLE_HOME/rdbms/admin/utlxplan.sql,然后在当前用户SQL>下运行,它创建一个plan_table,用来存储分析SQL语句的结果。
  create table PLAN_TABLE (
   statement_id    varchar2(30),
   timestamp       date,
   remarks         varchar2(80),
   operation       varchar2(30),
   options         varchar2(30),
   object_node     varchar2(128),
   object_owner    varchar2(30),
   object_name     varchar2(30),
   object_instance numeric,
   object_type     varchar2(30),
   optimizer       varchar2(255),
   search_columns  number,
   id              numeric,
   parent_id       numeric,
   position        numeric,
   cost            numeric,
   cardinality     numeric,
   bytes           numeric,
   other_tag       varchar2(255),
   partition_start varchar2(255),
   partition_stop  varchar2(255),
   partition_id    numeric,
   other           long,
   distribution    varchar2(30));
    在SQL/PLUS的窗口运行以下命令
  set time on;   (说明:打开时间显示)
  set autotrace on;   (说明:打开自动分析统计,并显示SQL语句的运行结果)
  set autotrace traceonly;  (说明:打开自动分析统计,不显示SQL语句的运行结果)
    接下来你就运行测试SQL语句,看到其分析统计结果了。一般来讲,我们的SQL语句应该避免对大表的全表扫描。
    关闭以上功能,在SQL/PLUS的窗口运行以下命令
  set time off;   (说明:关闭时间显示)
  set autotrace off;   (说明:关闭自动分析统计)

 

评论】 【关闭
相关新闻
Google提供广告