1. SQL> create or replace procedure sql_explain(v_sql varchar2)  
  2.   2  is  
  3.   3  type explain_cursor_type is ref cursor;  
  4.   4  explain_cursor explain_cursor_type;  
  5.   5  a varchar2(2048);  
  6.   6  begin  
  7.   7    execute immediate 'explain plan for '||v_sql;  
  8.   8    open explain_cursor for select PLAN_TABLE_OUTPUT  from table(dbms_xplan.display());  
  9.   9    loop  
  10.  10      fetch explain_cursor into a;  
  11.  11      exit when explain_cursor%NOTFOUND;  
  12.  12     dbms_output.put_line(a);  
  13.  13    end loop;  
  14.  14  end;  
  15.  15  /  
  16.   
  17. Procedure created.  
  18. SQL> exec sql_explain('select a.name,b.name from t1 a,t2 b where a.id=b.id and a.id=1');  
  19. Plan hash value: 2680223496  
  20. --------------------------------------------------------------------------------------  
  21. | Id  | Operation                    | Name  | Rows  | Bytes | Cost (%CPU)| Time     |  
  22. --------------------------------------------------------------------------------------  
  23. |   0 | SELECT STATEMENT             |       |     1 |    17 |     4   (0)| 00:00:01 |  
  24. |   1 |  NESTED LOOPS                |       |     1 |    17 |     4   (0)| 00:00:01 |  
  25. |   2 |   TABLE ACCESS BY INDEX ROWID| T1    |     1 |     8 |     1   (0)| 00:00:01 |  
  26. |*  3 |    INDEX UNIQUE SCAN         | T1_PK |     1 |       |     0   (0)| 00:00:01 |  
  27. |*  4 |   TABLE ACCESS FULL          | T2    |     1 |     9 |     3   (0)| 00:00:01 |  
  28. --------------------------------------------------------------------------------------  
  29. Predicate Information (identified by operation id):  
  30. ---------------------------------------------------  
  31. 3 - access("A"."ID"=1)  
  32. 4 - filter("B"."ID"=1)  
  33.   
  34. PL/SQL procedure successfully completed.  
  35.   
  36. SQL> exec sql_explain('select a.name,b.name from t1 a,t2 b where a.id=b.id and a.id=''1''');  
  37. Plan hash value: 2680223496  
  38. --------------------------------------------------------------------------------------  
  39. | Id  | Operation                    | Name  | Rows  | Bytes | Cost (%CPU)| Time     |  
  40. --------------------------------------------------------------------------------------  
  41. |   0 | SELECT STATEMENT             |       |     1 |    17 |     4   (0)| 00:00:01 |  
  42. |   1 |  NESTED LOOPS                |       |     1 |    17 |     4   (0)| 00:00:01 |  
  43. |   2 |   TABLE ACCESS BY INDEX ROWID| T1    |     1 |     8 |     1   (0)| 00:00:01 |  
  44. |*  3 |    INDEX UNIQUE SCAN         | T1_PK |     1 |       |     0   (0)| 00:00:01 |  
  45. |*  4 |   TABLE ACCESS FULL          | T2    |     1 |     9 |     3   (0)| 00:00:01 |  
  46. --------------------------------------------------------------------------------------  
  47. Predicate Information (identified by operation id):  
  48. ---------------------------------------------------  
  49. 3 - access("A"."ID"=1)  
  50. 4 - filter("B"."ID"=1)  
  51.   
  52. PL/SQL procedure successfully completed.  
SQL> create or replace procedure sql_explain(v_sql varchar2)
  2  is
  3  type explain_cursor_type is ref cursor;
  4  explain_cursor explain_cursor_type;
  5  a varchar2(2048);
  6  begin
  7    execute immediate 'explain plan for '||v_sql;
  8    open explain_cursor for select PLAN_TABLE_OUTPUT  from table(dbms_xplan.display());
  9    loop
 10      fetch explain_cursor into a;
 11      exit when explain_cursor%NOTFOUND;
 12     dbms_output.put_line(a);
 13    end loop;
 14  end;
 15  /

Procedure created.
SQL> exec sql_explain('select a.name,b.name from t1 a,t2 b where a.id=b.id and a.id=1');
Plan hash value: 2680223496
--------------------------------------------------------------------------------------
| Id  | Operation                    | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |       |     1 |    17 |     4   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                |       |     1 |    17 |     4   (0)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| T1    |     1 |     8 |     1   (0)| 00:00:01 |
|*  3 |    INDEX UNIQUE SCAN         | T1_PK |     1 |       |     0   (0)| 00:00:01 |
|*  4 |   TABLE ACCESS FULL          | T2    |     1 |     9 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("A"."ID"=1)
4 - filter("B"."ID"=1)

PL/SQL procedure successfully completed.

SQL> exec sql_explain('select a.name,b.name from t1 a,t2 b where a.id=b.id and a.id=''1''');
Plan hash value: 2680223496
--------------------------------------------------------------------------------------
| Id  | Operation                    | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |       |     1 |    17 |     4   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                |       |     1 |    17 |     4   (0)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| T1    |     1 |     8 |     1   (0)| 00:00:01 |
|*  3 |    INDEX UNIQUE SCAN         | T1_PK |     1 |       |     0   (0)| 00:00:01 |
|*  4 |   TABLE ACCESS FULL          | T2    |     1 |     9 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("A"."ID"=1)
4 - filter("B"."ID"=1)

PL/SQL procedure successfully completed.

第二种:添加format参数,灵活选择

  1. SQL> create or replace procedure sql_explain(v_sql varchar2,v_format varchar2)  
  2.   2  is  
  3.   3  type explain_cursor_type is ref cursor;  
  4.   4  explain_cursor explain_cursor_type;  
  5.   5  a varchar2(2048);  
  6.   6  begin  
  7.   7    execute immediate 'explain plan for '||v_sql;  
  8.   8    open explain_cursor for select PLAN_TABLE_OUTPUT  from table(dbms_xplan.display(null,null,v_format));  
  9.   9    loop  
  10.  10      fetch explain_cursor into a;  
  11.  11      exit when explain_cursor%NOTFOUND;  
  12.  12     dbms_output.put_line(a);  
  13.  13    end loop;  
  14.  14  end;  
  15.  15  /  
  16.   
  17. Procedure created.  
  18. SQL> exec sql_explain('select a.name,b.name from t1 a,t2 b where a.id=b.id and a.id=1','all');  
  19. Plan hash value: 2680223496  
  20. --------------------------------------------------------------------------------------  
  21. | Id  | Operation                    | Name  | Rows  | Bytes | Cost (%CPU)| Time     |  
  22. --------------------------------------------------------------------------------------  
  23. |   0 | SELECT STATEMENT             |       |     1 |    17 |     4   (0)| 00:00:01 |  
  24. |   1 |  NESTED LOOPS                |       |     1 |    17 |     4   (0)| 00:00:01 |  
  25. |   2 |   TABLE ACCESS BY INDEX ROWID| T1    |     1 |     8 |     1   (0)| 00:00:01 |  
  26. |*  3 |    INDEX UNIQUE SCAN         | T1_PK |     1 |       |     0   (0)| 00:00:01 |  
  27. |*  4 |   TABLE ACCESS FULL          | T2    |     1 |     9 |     3   (0)| 00:00:01 |  
  28. --------------------------------------------------------------------------------------  
  29. Query Block Name / Object Alias (identified by operation id):  
  30. -------------------------------------------------------------  
  31. 1 - SEL$1  
  32. 2 - SEL$1 / A@SEL$1  
  33. 3 - SEL$1 / A@SEL$1  
  34. 4 - SEL$1 / B@SEL$1  
  35. Predicate Information (identified by operation id):  
  36. ---------------------------------------------------  
  37. 3 - access("A"."ID"=1)  
  38. 4 - filter("B"."ID"=1)  
  39. Column Projection Information (identified by operation id):  
  40. -----------------------------------------------------------  
  41. 1 - (#keys=0) "A"."NAME"[VARCHAR2,32], "B"."NAME"[VARCHAR2,32]  
  42. 2 - "A"."NAME"[VARCHAR2,32]  
  43. 3 - "A".ROWID[ROWID,10]  
  44. 4 - "B"."NAME"[VARCHAR2,32]  
  45.   
  46. PL/SQL procedure successfully completed.  
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐