本帖最后由 郑全 于 2020-3-11 01:06 编辑
第三种,使用 set autotrace on 方式,该方式,SQL语句会执行。
关于Autotrace几个常用选项的说明:
SET AUTOTRACE OFF ---------------- 不生成AUTOTRACE 报告,这是缺省模式
SET AUTOTRACE ON EXPLAIN ------ AUTOTRACE只显示优化器执行路径报告
SET AUTOTRACE ON STATISTICS -- 只显示执行统计信息
SET AUTOTRACE ON ----------------- 包含执行计划和统计信息
SET AUTOTRACE TRACEONLY ------ 同set autotrace on,但是不显示查询
执行步骤
如果是普通用户需要按以下方式执行:
一.创建基础表
1.创建PLAN_TABLE表
sql>connect / as sysdba;
@?/rdbms/admin/utlxplan;
2.为了使多个用户都可以共享同一个PLAN_TABLE,为它创建一个公共同义词,并授权给PUBLIC
create public synonym plan_table for plan_table;
grant all on plan_table to public;
二.创建PLUSTRACE角色
1. @?/sqlplus/admin/plustrce
2.把PLUSTRACE角色授予PUBLIC,这样所有用户都拥有PLUSTRACE角色得权限.
grant plustrace to public;
三.使用AUTOTRACE
SQL> conn hr/hr
Connected.
SQL> set autotrace on
SQL>
SQL> select e.employee_id,e.last_name,d.department_name
2 from employees e, departments d
3 where e.department_id=d.department_id
4 and d.department_id=20;
EMPLOYEE_ID LAST_NAME DEPARTMENT_NAME
----------- ------------------------- ------------------------------
201 Hartstein Marketing
202 Fay Marketing
Execution Plan
----------------------------------------------------------
Plan hash value: 1475904561
----------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 62 | 2 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 2 | 62 | 2 (0)| 00:00:01 |
| 2 | TABLE ACCESS BY INDEX ROWID | DEPARTMENTS | 1 | 16 | 1 (0)| 00:00:01 |
|* 3 | INDEX UNIQUE SCAN | DEPT_ID_PK | 1 | | 0 (0)| 00:00:01 |
| 4 | TABLE ACCESS BY INDEX ROWID BATCHED| EMPLOYEES | 2 | 30 | 1 (0)| 00:00:01 |
|* 5 | INDEX RANGE SCAN | EMP_DEPARTMENT_IX | 2 | | 0 (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("D"."DEPARTMENT_ID"=20)
5 - access("DEPARTMENT_ID"=20)
Statistics
----------------------------------------------------------
511 recursive calls
14 db block gets
733 consistent gets
11 physical reads
2780 redo size
783 bytes sent via SQL*Net to client
552 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
42 sorts (memory)
0 sorts (disk)
2 rows processed
普通用户,显得有点繁琐。
贴图:
|