数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

如何分析SQL语句


发布日期:2022年03月21日
 
如何分析SQL语句

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

先在sys用户下运行@/ORACLE_HOME/sqlplus/admin/plustrcesql

内容

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/utlxplansql然后在当前用户SQL>下运行它创建一个plan_table用来存储分析SQL语句的结果

create table PLAN_TABLE (

statement_idvarchar()

timestamp date

remarks varchar()

operation varchar()

options varchar()

object_node varchar()

object_ownervarchar()

object_name varchar()

object_instance numeric

object_type varchar()

optimizer varchar()

search_columnsnumber

idnumeric

parent_id numeric

positionnumeric

costnumeric

cardinality numeric

bytes numeric

other_tag varchar()

partition_start varchar()

partition_stopvarchar()

partition_idnumeric

other long

distributionvarchar());

在SQL/PLUS的窗口运行以下命令

set time on;(说明打开时间显示)

set autotrace on;(说明打开自动分析统计并显示SQL语句的运行结果)

set autotrace traceonly;(说明打开自动分析统计不显示SQL语句的运行结果)

接下来你就运行测试SQL语句看到其分析统计结果了一般来讲我们的SQL语句应该避免对大表的全表扫描

关闭以上功能在SQL/PLUS的窗口运行以下命令

set time off;(说明关闭时间显示)

set autotrace off;(说明关闭自动分析统计)

上一篇:Oracle9i 安装手记

下一篇:Oracle冷备和热备脚本