脚踏实地、勇往直前!
全部博文(1005)
分类: Oracle
2012-07-18 12:05:30
今天有个同事问我,使用分批提取的游标时候,是执行一次多次提取,还是执行多次多次提取,我直接告诉他是只执行一次游标的SQL,多次提取数据集.以下是演示过程.
1.获取该会话的SID
SQL> connect / as sysdba
已连接。
SQL> SELECT SYS_CONTEXT ('USERENV', 'SID') sid FROM DUAL;
SID
--------------------------------------------------------------------------------
141
2.启动会话跟踪
SQL> alter session set sql_trace=true;
会话已更改。
3.执行如下过程
SQL> Declare
2 Cursor Cur_Test Is
3 Select Object_Id From Dba_Objects t;
4 Type Ty_Table_Object_Id Is Table Of Dba_Objects.Object_Id%Type;
5 l_Ty_Table_Object_Id Ty_Table_Object_Id;
6 Begin
7 Open Cur_Test;
8 Loop
9 Fetch Cur_Test Bulk Collect
10 Into l_Ty_Table_Object_Id Limit 1000;
11 Null;
12 Exit When Cur_Test%Notfound Or Cur_Test%Notfound Is Null;
13 End Loop;
14 Close Cur_Test;
15 End;
16 /
PL/SQL 过程已成功完成。
4.结束会话跟踪
SQL> alter session set sql_trace=false;
会话已更改。
5.获取生成的trace文件
SQL> Select Rtrim(c.Value, '/') || Decode(e.Plat_Id, 1, '\', '/') ||
2 d.Instance_Name || '_ora_' || Ltrim(To_Char(a.Spid)) || '.trc' Trace
_File
3 From V$process a,
4 V$session b,
5 V$parameter c,
6 V$instance d,
7 (Select Case
8 When t.Banner Like '%Windows%' Then
9 1
10 When t.Banner Like '%Linux%' Then
11 2
12 When t.Banner Like '%AIX%' Then
13 3
14 When t.Banner Like '%Solaris%' Then
15 4
16 End Plat_Id
17 From V$version t
18 Where t.BANNER Like '%TNS%'
19 ) e
20 Where a.Addr = b.Paddr
21 And b.Sid = 141
22 And c.Name = 'user_dump_dest';
TRACE_FILE
--------------------------------------------------------------------------------
d:\app\diag\rdbms\oracl\oracl\trace\oracl_ora_1096.trc
6.使用tkprof格式输出
>tkprof d:\app\diag\rdbms\oracl\oracl\trace\oracl_ora_1096.trc D:\tkprof\output03.txt
7.从output03.txt可以找到如下输出部分
SQL ID : gtfh0nwy1wnnv
SELECT OBJECT_ID
FROM
DBA_OBJECTS T
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.01 0.01 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 69 0.25 0.25 0 1692 0 68306
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 71 0.26 0.26 0 1692 0 68306
从以上输出可以看出,该语句只执行了一次,但提取了69次.