Chinaunix首页 | 论坛 | 博客
  • 博客访问: 504218
  • 博文数量: 240
  • 博客积分: 791
  • 博客等级: 军士长
  • 技术积分: 1694
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-23 18:21
文章分类

全部博文(240)

文章存档

2013年(21)

2012年(219)

我的朋友

分类:

2012-05-31 09:08:57

碰到很多朋友再问SQL语句优化的问题,发现比较多的人不会查看SQL语句的执行计划, 
在此开贴   简单介绍   
                                    如何分析SQL语句 
        很多时候,我们不太清楚自己写的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; (说明:关闭自动分析统计) 

---for   example: 
  我已有用户IFSAPP想在此用户下查看执行计划 
--创建角色 
ifsapp@PRACTICE> conn   sys/practice   as   sysdba; 
已连接。 

ifsapp@PRACTICE> @e:\oracle\ora92\sqlplus\admin\plustrce.sql 
ifsapp@PRACTICE> 
ifsapp@PRACTICE> drop   role   plustrace; 
drop   role   plustrace 
                    * 
ERROR   位于第   1   行: 
ORA-01919:   角色 'PLUSTRACE '不存在 


ifsapp@PRACTICE> create   role   plustrace; 

角色已创建 

ifsapp@PRACTICE> 
ifsapp@PRACTICE> grant   select   on   v_$sesstat   to   plustrace; 

授权成功。 

ifsapp@PRACTICE> grant   select   on   v_$statname   to   plustrace; 

授权成功。 

ifsapp@PRACTICE> grant   select   on   v_$session   to   plustrace; 

授权成功。 

ifsapp@PRACTICE> grant   plustrace   to   dba   with   admin   option; 

授权成功。 

ifsapp@PRACTICE> 
ifsapp@PRACTICE> set   echo   off 
ifsapp@PRACTICE> grant   plustrace   to   ifsapp; 

授权成功。 

ifsapp@PRACTICE> conn   ifsapp/ifsapp; 
已连接。 

--创建当前用户下的PLAN_TABLE 
ifsapp@PRACTICE> @e:\oracle\ora92\rdbms\admin\utlxplan.sql 

表已创建。 

ifsapp@PRACTICE> set   autotrace   on 
ifsapp@PRACTICE> select   *   from   PLAN_TABLE; 

未选定行 


--以下就是执行计划的内容:) 

Execution   Plan 
---------------------------------------------------------- 
      0             SELECT   STATEMENT   Optimizer=CHOOSE 
      1         0       TABLE   ACCESS   (FULL)   OF   'PLAN_TABLE ' 


Statistics 
---------------------------------------------------------- 
                    0     recursive   calls 
                    0     db   block   gets 
                    3     consistent   gets 
                    0     physical   reads 
                    0     redo   size 
              1970     bytes   sent   via   SQL*Net   to   client 
                503     bytes   received   via   SQL*Net   from   client 
                    2     SQL*Net   roundtrips   to/from   client 
                    0     sorts   (memory) 
                    0     sorts   (disk) 
                    0     rows   processed 
阅读(580) | 评论(0) | 转发(0) |
0

上一篇:Nginx优化参数

下一篇:Nginx与PHP的整合实战

给主人留下些什么吧!~~