Oracle

Oracle 如何獲得在 PLSQL 內部執行的 SQL 的執行計劃?

  • May 26, 2022

是否可以檢查在 PLSQL 塊內執行的 SQL 語句的執行計劃?

DECLARE
   l_count PLS_INTEGER;
BEGIN
   SELECT COUNT(1) INTO l_count
   FROM foo;
END;
/

對於正常 SQL,我通常會執行以下命令來檢查執行計劃:

select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));

但是,這只是報告:

NOTE: cannot fetch plan for SQL_ID: 3q0sujncq54wy, CHILD_NUMBER: 0
     Please verify value of SQL_ID and CHILD_NUMBER; 
     It could also be that the plan is no longer in cursor cache (check v$sql_plan)

您可以查找sql_idAnd child_numberin v$sql。就像是:

Select s.sql_id, s.child_number
From   V$sql s
Where  upper(sql_text) like upper('SELECT COUNT(%FROM foo')
And    Sql_text not like '%v$sql%';

請注意,pl/sql 為您規範化靜態 SQL 以提高游標共享 - 它將是大寫的,空格更少。

一旦你有了這些,你就可以輸入到dbms_xplan.display_cursor函式中

引用自:https://dba.stackexchange.com/questions/312623