Home » RDBMS Server » Server Administration » explain plan using dynamic sql (merged threads)
explain plan using dynamic sql (merged threads) [message #127872] Wed, 13 July 2005 16:33 Go to next message
Hina
Messages: 51
Registered: April 2004
Member
Can dynamic sql be used to generate explan plan ?

I am using following syntax and giving me an error.
What and how to use it ?

EXPLAIN PLAN
SET STATEMENT_ID = '123'
FOR
my_sql;

Regards
Re: explain plan using dynaimc sql [message #127899 is a reply to message #127872] Thu, 14 July 2005 00:41 Go to previous messageGo to next message
Frank Naude
Messages: 4587
Registered: April 1998
Senior Member
SQL> BEGIN
  2    EXECUTE IMMEDIATE 'EXPLAIN PLAN SET STATEMENT_ID=''123'' FOR SELECT * FROM DUAL';
  3  END;
  4  /

PL/SQL procedure successfully completed.

SQL>
SQL> SELECT timestamp, depth, operation, options, object_name
  2    FROM plan_table
  3   WHERE statement_id = '123';

TIMESTAMP      DEPTH OPERATION                      OPTIONS    OBJECT_NAM
--------- ---------- ------------------------------ ---------- ----------
14-JUL-05          0 SELECT STATEMENT
14-JUL-05          1 TABLE ACCESS                   FULL       DUAL


Best regards.

Frank
Re: explain plan using dynaimc sql [message #127984 is a reply to message #127899] Thu, 14 July 2005 08:24 Go to previous messageGo to next message
Hina
Messages: 51
Registered: April 2004
Member
How to use dynamic sql rather then 'SELECT * FROM DUAL'
e.g.

my_sql := 'select * from dual';

EXECUTE IMMEDIATE 'EXPLAIN PLAN SET STATEMENT_ID=''222'' FOR my_sql', it it ok ?
Re: explain plan using dynaimc sql [message #127994 is a reply to message #127872] Thu, 14 July 2005 09:00 Go to previous messageGo to next message
smartin
Messages: 1803
Registered: March 2005
Location: Jacksonville, Florida
Senior Member
MYDBA@ORCL > declare
  2  sql_txt varchar2(200);
  3  begin
  4  sql_txt := 'select * from dual';
  5  execute immediate 'explain plan for ' || sql_txt;
  6  end;
  7  /

PL/SQL procedure successfully completed.

MYDBA@ORCL > select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------

Plan hash value: 272002086

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |     2 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |
--------------------------------------------------------------------------

8 rows selected.

MYDBA@ORCL >

ORA-06512 at line 4 [message #127997 is a reply to message #127872] Thu, 14 July 2005 09:10 Go to previous messageGo to next message
Hina
Messages: 51
Registered: April 2004
Member
Whats wrong with following statements ?

declare
mysql varchar2(30) := 'select sysdate from dual';
begin
execute immediate 'explain plan set statement_id = ''323'' for '''||mysql||''' ';
end;
Re: ORA-06512 at line 4 [message #127998 is a reply to message #127997] Thu, 14 July 2005 09:14 Go to previous messageGo to next message
smartin
Messages: 1803
Registered: March 2005
Location: Jacksonville, Florida
Senior Member
Please stick with your original thread on Server Administration, we'll get it covered.

http://www.orafaq.com/forum/t/48747/67982/
Re: explain plan using dynaimc sql [message #128003 is a reply to message #127899] Thu, 14 July 2005 09:36 Go to previous messageGo to next message
Hina
Messages: 51
Registered: April 2004
Member
Whats wrong with following statements ?

declare
mysql varchar2(30) := 'select sysdate from dual';
begin
execute immediate 'explain plan set statement_id = ''323'' for '''||mysql||''' ';
end;
Re: explain plan using dynaimc sql [message #128015 is a reply to message #127872] Thu, 14 July 2005 10:27 Go to previous message
smartin
Messages: 1803
Registered: March 2005
Location: Jacksonville, Florida
Senior Member
You have too many quotes around the mysql part at the right. See how I built the string above? You don't need to surround the varchar2 variable in quotes, only constant text.
Previous Topic: selective exporting
Next Topic: cluster table size
Goto Forum:
  


Current Time: Fri Jan 10 07:56:58 CST 2025