基础环境
操作系统:Red Hat Enterprise Linux Server release 7.9 (Maipo)
数据库版本:DM Database Server 64 V8
架构:单实例
1 set autotrace trace
SQL> set autotrace trace
SQL> select object_id from t3 where object_id='268436636';
行号 OBJECT_ID
---------- ---------
1 268436636
1 #NSET2: [1, 23, 38]
2 #PRJT2: [1, 23, 38]; exp_num(2), is_atom(FALSE)
3 #SSEK2: [1, 23, 38]; scan_type(ASC), IDX_T3_OBJECT_ID(T3)
已用时间: 0.275(毫秒). 执行号:2006.
2 explain
SQL> explain
2 select object_id from t3 where object_id='268436636';
1 #NSET2: [1, 23, 38]
2 #PRJT2: [1, 23, 38]; exp_num(2), is_atom(FALSE)
3 #SSEK2: [1, 23, 38]; scan_type(ASC), IDX_T3_OBJECT_ID(T3), scan_range[exp_cast('268436636'),exp_cast('268436636')]
已用时间: 0.673(毫秒). 执行号:0.
3 v$cachepln
v$cachepln中保存了SQL缓冲区中的执行计划信息,但只有在ini参数USE_PLN_POOL !=0时才统计。根据v$cachepln中的cache_item可以获取实际执行计划信息。
--查看参数USE_PLN_POOL
SQL> select name,value,sys_value,file_value from v$parameter where name='USE_PLN_POOL';
行号 NAME VALUE SYS_VALUE FILE_VALUE
---------- ------------ ----- --------- ----------
1 USE_PLN_POOL 1 1 1
--查出cache_item
SQL> select cache_item,sqlstr from v$cachepln where sqlstr like '%select object_id from t3 where %';
行号 CACHE_ITEM SQLSTR
---------- -------------------- -----------------------------------------------------------------------------------------------
2 139674267535472 select object_id from t3 where object_id='268436636';
--生成Trace
SQL> alter session set events 'immediate trace name plndump level 139674267535472,dump_file ''/tmp/sql_139674267535472.log''';
操作已执行
已用时间: 3.925(毫秒). 执行号:2007.
--查看trace内容
[dmdba@test1 audit]$ cat /tmp/sql_139674267535472.log
DM Database Server x64 V8
*** 2023-09-03 03:55:31.139000*** start dump the infos of pln[139674267535472].
start dump the infos of pln[139674267535472].
SQL_STR:
select object_id from t3 where object_id='268436636';
PLN_CMD:
0 savepoint
6 dop_try_begin 0
10 dop_try_begin 1
14 sql 0 0
24 nop
26 jmp 67
32 nop
34 push 0
40 swap
42 sloc 1
46 err_set 0
50 rollback
56 jmp 67
62 nop
64 throw dir 1
67 exception end
69 savepoint
75 cop 'b'
79 hlt 0
sqlnode[0]::::
1 #NSET2: [1, 23, 38]
2 #PRJT2: [1, 23, 38]; exp_num(2), is_atom(FALSE)
3 #SSEK2: [1, 23, 38]; scan_type(ASC), IDX_T3_OBJECT_ID(T3)
end dump the infos of pln[139674267535472].
4 ET系统函数
官方文档
https://eco.dameng.com/document-preview/dm/zh-cn/ops/performance-et
ET 工具是 DM 数据库自带的 SQL 性能分析工具,能够统计 SQL 语句执行过程中每个操作符的实际开销,为 SQL 优化提供依据以及指导。
ET函数统计对应执行ID的所有操作符的执行时间。使用ET函数需设置INI参数ENABLE_MONITOR=1、MONITOR_TIME=1、MONITOR_SQL_EXEC=1
--查看参数
SQL> select name,value,sys_value,file_value from v$parameter where name in ('ENABLE_MONITOR','MONITOR_TIME','MONITOR_SQL_EXEC');
行号 NAME VALUE SYS_VALUE FILE_VALUE
---------- ---------------- ----- --------- ----------
1 ENABLE_MONITOR 1 1 1
2 MONITOR_TIME 1 1 1
3 MONITOR_SQL_EXEC 0 0 0
已用时间: 6.694(毫秒). 执行号:526.
--设置当前会话MONITOR_SQL_EXEC参数为1
SQL> alter session set 'MONITOR_SQL_EXEC'=1;
DMSQL 过程已成功完成
已用时间: 1.024(毫秒). 执行号:2008.
--执行对应的sql,并根据执行号查看执行计划
SQL> select object_id from t3 where object_id='268436636';
行号 OBJECT_ID
---------- ---------
1 268436636
已用时间: 0.282(毫秒). 执行号:2012.
SQL> ET(2012);
行号 OP TIME(US) PERCENT RANK SEQ N_ENTER HASH_USED_CELLS HASH_CONFLICT
---------- ----- -------------------- ------- -------------------- ----------- ----------- -------------------- --------------------
1 DLCK 1 1.72% 4 0 2 0 0
2 PRJT2 2 3.45% 3 2 4 0 0
3 NSET2 15 25.86% 2 1 3 0 0
4 SSEK2 40 68.97% 1 3 2 0 0
已用时间: 12.000(毫秒). 执行号:2014.
ET 结果说明
OP: 操作符
TIME(us): 时间开销,单位为微秒
PERCENT: 执行时间占总时间百分比
RANK: 执行时间耗时排序
SEQ: 执行计划节点号
N_ENTER: 进入次数
5 dbms_sqltune包
官方文档
《DM8系统包使用手册》
40 DBMS_SQLTUNE 包
DBMS_SQLTUNE系统包兼容Oracle的DBMS_SQLTUNE包的部分功能,提供一系列对实时SQL监控的方法。
当SQL监控功能开启后,DBMS_SQLTUNE包可以实时监控SQL执行过程中的信息,包括:执行时间、执行代价、执行用户、统计信息等情况。使用DBMS_SQLTUNE也需要将DM.INI参数ENABLE_MONITOR、MONITOR_TIME、MONITOR_SQL_EXEC设置为1。
使用DBMS_SQLTUNE.REPORT_SQL_MONITOR方法可以查看上述执行号为2012的执行计划信息。DBMS_SQLTUNE方法获取的执行计划比ET函数更详细。
SQL> set long 100000
SQL> select dbms_sqltune.report_sql_monitor(sql_exec_id=>2012);
行号 DBMS_SQLTUNE.REPORT_SQL_MONITOR(SQL_EXEC_ID=>2012)
---------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 SQL Monitoring Report
SQL Text
------------------------------
select object_id from t3 where object_id='268436636';
Global Information
------------------------------
Status : DONE (ALL ROWS)
Session : TEST (145947392:14)
SQL ID : 204
SQL Execution ID : 2012
Execution Started : 2023-09-03 04:05:05
Duration : 0.000123s
Program : disql
Global Stats
=========================================================
| Affected | Bytes | Bytes | Physical | Logical |
| Rows | Allocate | Free | Read(page) | Read(page) |
=========================================================
| 0 | 0 | 0 | 0 | 2 |
=========================================================
SQL Plan
------------------------------
1 #NSET2: [1, 23, 38]
2 #PRJT2: [1, 23, 38]; exp_num(2), is_atom(FALSE)
3 #SSEK2: [1, 23, 38]; scan_type(ASC), IDX_T3_OBJECT_ID(T3)
SQL Plan Monitoring Details
==================================================================================================================
| Id | Operation | Name | Rows | Cost | Time | Start | Execs | Rows | Activity | Activity Detail |
| | | | (Estim) | | Active(s) | Active | | (Actual) | (%) | (# samples) |
==================================================================================================================
| 0 | DLCK | | | | 0.000001 | +0.000076 | 2 | | 1.72 | |
| 1 | NSET2 | | 23 | 1 | 0.000015 | +0.000075 | 3 | 1 | 25.86 | |
| 2 | PRJT2 | | 23 | 1 | 0.000002 | +0.000075 | 4 | 1 | 3.45 | |
| 3 | SSEK2 | T3 | 23 | 1 | 0.000040 | +0.000074 | 2 | 1 | 68.97 | |
==================================================================================================================
已用时间: 13.378(毫秒). 执行号:2015.
SQL>
需要修改参数ENABLE_MONITOR、MONITOR_TIME、MONITOR_SQL_EXEC为1
6 官方文档
执行计划中每一步的解释可以看官方文档。
《DM8系统管理员手册》
附录 4 执行计划操作符
谨记:心存敬畏,行有所止。