1. 檢查數(shù)據(jù)庫中的 OPEN_CURSORS 參數(shù)值。 Oracle 使用 init.ora 中的初始化參數(shù) OPEN_CURSORS 指定一個會話一次最多可以擁有的游標(biāo)數(shù)。缺省值為 50。要獲得數(shù)據(jù)庫中 OPEN_CURSORS 參數(shù)的值,可以使用以下查詢: SQL> show parameter open_cursors; NAME TYPE VALUE ------------------------------------ ----------- --------------- open_cursors integer 1000
重要的是將 OPEN_CURSORS 的值設(shè)置得足夠大,以避免應(yīng)用程序用盡所有打開的游標(biāo)。應(yīng)用程序不同,該值也不同。即便會話打開的游標(biāo)數(shù)未達(dá) OPEN_CURSORS 指定的數(shù)量(即設(shè)置的值高于實際需要的值), 也不會增加系統(tǒng)開銷。 2. 獲取打開的游標(biāo)數(shù)。 下面的查詢按降序顯示用戶“SCOTT”為每個會話打開的游標(biāo)數(shù)。 SQL> select o.sid, osuser, machine, count(*) num_curs 2 from v$open_cursor o, v$session s 3 where user_name = 'SCOTT' and o.sid=s.sid 4 group by o.sid, osuser, machine 5 order by num_curs desc; SID OSUSER MACHINE NUM_CURS ----------------------------------------------------- 217 m1 1000 96 m2 10 411 m3 10 50 test 9 請注重,v$open_cursor 可以跟蹤會話中 PARSED 和 NOT CLOSED 的動態(tài)游標(biāo)(使用 dbms_sql.open_cursor() 打開的游標(biāo))。它不會跟蹤未經(jīng)分析(但已打開)的動態(tài)游標(biāo)。在應(yīng)用程序中使用動態(tài)游標(biāo)并不常見。本模式的前提是未使用動態(tài)游標(biāo)。 3. 獲取為游標(biāo)執(zhí)行的 SQL。 使用在以上查詢結(jié)果中找到的 SID 運行下面的查詢: SQL> select q.sql_text 2 from v$open_cursor o, v$sql q 3 where q.hash_value=o.hash_value and o.sid = 217; SQL_TEXT select * from empdemo where empid='212' select * from empdemo where empid='321' select * from empdemo where empid='947' select * from empdemo where empid='527' ... 結(jié)果將顯示正在連接上執(zhí)行的查詢。它提供了一個入手點,讓您可以反向跟蹤到打開游標(biāo)的來源。