Measuring/Monitoring 每次执行的响应时间 sql

Measuring/Monitoring response time of each execution of a sql

在Oracle 11g中,我想监控特定sql的响应时间(我知道sql的sql_id)。意思是,我想知道 sql 每次执行时的响应时间。我会 "turn it on" 一段时间,然后希望看到类似这样的数据:

For sql_id 'abcdefg', following were the execution time (in ms) 10 12 10 13 10 10 10 240 230 10 9 12 … …

我们可以忽略我希望看到的输出的确切格式,但是有没有什么方法可以让我监控,以便我可以获得 sql 每次执行的响应时间?

谢谢。

首先,您应该跟踪您的 sql(假设 sql_id:6b0z40gs9m759):

SQL> ALTER SESSION SET sql_trace=TRUE;

SQL> ALTER SESSION SET EVENTS 'trace[rdbms.SQL_Optimizer.*][sql:6b0z40gs9m759]';
[ SQL> ALTER SYSTEM SET EVENTS 'sql_trace [sql:&&sql_id] bind=true, wait=true'; ] -- or use this as alternative.

SQL> ALTER SESSION SET TRACEFILE_IDENTIFIER = "TrcSqlParag"; -- adds this text to your trace file and makes it more recognizable by you.

SQL> SHOW PARAMETER user_dump_dest -- trace files are produced in this directory

-- For 11g DB, you may easily query v$diag_info view to see your file destination :
SQL> SELECT value FROM   v$diag_info WHERE  name = 'Default Trace File';

-- You can identify the trace file for a specific session using the V$SESSION and V$PROCESS views :
SQL> SELECT p.tracefile FROM v$session s JOIN v$process p ON s.paddr = p.addr WHERE  s.sid = &ses_id;

-- You may finish tracing whenever you want :
SQL> ALTER SESSION SET sql_trace=FALSE; 

让我们有这样一个转储文件:

/u01/app/oracle/admin/MYDB11G/udump/mydb11g_ora_TrcSqlParag.trc

跟踪文件不容易读取,我们需要tkprof utility从OS读取它:

$ cd /u01/app/oracle/admin/MYDB11G/udump/
$ tkprof mydb11g_ora_TrcSqlParag.trc TrcSqlParag_Translated1.txt explain=<username>/<pwd>@mydb11g table=sys.plan_table sys=no waits=yes