设为首页 加入收藏

TOP

查询sql语句的执行计划(一)
2014-11-24 02:49:32 来源: 作者: 【 】 浏览:3
Tags:查询 sql 语句 执行 计划
查询sql语句的执行计划
碰到很多朋友再问SQL语句优化的问题,发现比较多的人不会查看SQL语句的执行计划,
在此开贴 简单介绍 www.2cto.com
如何分析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
www.2cto.com
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的窗口运行以下命令
www.2cto.com
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;
授权成功。 www.2cto.com
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;
未选定行
--以下就是执行计划的内容
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇堆表的在执行Select语句时的默认.. 下一篇堆表的在执行Select语句时的默认..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)
·Linux 常用命令集合 (2025-12-24 09:52:51)
·socket 编程如何实现 (2025-12-24 09:20:41)
·Python创建简易的Soc (2025-12-24 09:20:39)