Explain plan for a sql statement
Hi,
I have a simple sql statement which has a cost of 122, cardinality 59 and CPU cost 1966777.
Same sql statement written in another way (using a nested select in the 'from' clause), has a cost of 58, cardinality 24 but cpu cost is much higher 29078895.
What should I look for? For a better cost overall, or for a better cpu cost? Which statement should I consider? What is most important?
Thanks.
Roger25 wrote:
... Based on this, my question is what statement to choose? which is more important, the cost or the cpu cost?
Thanks.
Based on this, sorry, but my reply will remain unchanged:
Again, you cannot compare explain plan of two different queries.
Find out more:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:313416745628
Nicolas.
Similar Messages
-
Why tkprof did not show explain plan for some sql statements
Hi,
I did a trace for one of the session, hoping to find the explain plan for its tkprof. However, I only get this:
INSERT INTO AUDIT_TABLE
VALUES
( :B1 , :B2 , :B3 , :B4 , :B5 , :B6 , :B8 , :B7 )
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 3698 8.08 59.41 3348 566 19092 3698
Fetch 0 0.00 0.00 0 0 0 0
total 3699 8.08 59.42 3348 566 19092 3698
Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: CHOOSE
Parsing user id: 30 (recursive depth: 2)
********************************************************************************Any idea why the explain plan is missing?
Also looking at the above statistics, what can I conclude?Thanks John. What's ur take on this statistics then?
call count cpu elapsed disk query current rows
Parse 0 0.00 0.00 0 0 0 0
Execute 1798 12.23 36.75 1758 5404 7418 1798
Fetch 0 0.00 0.00 0 0 0 0
total 1798 12.23 36.75 1758 5404 7418 1798
Misses in library cache during parse: 0
Optimizer mode: CHOOSE
Parsing user id: 30 (recursive depth: 1)
******************************************************************************** -
Hi
I have done an explain for one of my sql and i got the result as follows
SELECT STATEMENT Cost = 502
SORT AGGREGATE
HASH JOIN
NESTED LOOPS
HASH JOIN
TABLE ACCESS FULL CMC_NWPE_RELATION
HASH JOIN
TABLE ACCESS FULL CMC_NWST_NET_SET
NESTED LOOPS
TABLE ACCESS FULL CMC_NWPR_RELATION
INDEX UNIQUE SCAN SYS_C006513
INDEX UNIQUE SCAN SYS_C006316
TABLE ACCESS FULL CMC_MEPR_PRIM_PROV
I need to know whether i have gone wrong with any of the joins in the sql. For the matter of fact I need to interpret the result of the explain plan for my sql.
Thanks in advance
Chandra SekharHi
First of all u haven't send the complete explanation in the plan table. As far i got from your result ur accesing the ful table scan CMC_NWPE_RELATION ,CMC_NWST_NET_SET , CMC_NWPR_RELATION & one more.
check in your auery why these table are accesing full when the data in this table will volumonuous ur system this query will become very slow as it will have to acces nosof rowsin table 1*nosof rowsin table 2*nosof rowsin table 3*nosof rowsin table 4 so u create the index in those tables and index should be made on the columns which r used in ur where clause of the query
Amit -
Hi ,
Our db version is Oracle 8i and looking for solution.
I have multiple select sql statments and need to get the explain plan for all statments.
Is it possible to take explain plan for all statments one time? If so please provide the steps.
Thanks.
Happy Halloween.Not sure exactly you are looking. But if you do
set autotrace on
from sqlplus and put all the query in a script, you should get all explain plan in one shot. You can spool the output to a file. -
Explain plans for PL/SQL code?
Hi!
I am pulling SQL statements (select, insert, update, delete, etc.) from PL/SQL code and producing explain plans. Some of the delete statements have "WHERE CURRENT OF" in them which produces an ORA-3001 error (feature not implemented) error. How can I do an explain plan of these statements? Can I replace "WHERE CURRENT OF" with "=" to get a plan? How far off will the plan be? Any suggestions, ideas, etc. greatfully appreciated!
This is Oracle 9.2.0.4/5 on AIX if it makes a difference.
Thanks!
Dave VenusWHERE CURRENT OF shouldn't be a problem. Here above an example on 9.2.0.4 on AIX5.2 :
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.4.0 - 64bit Production
With the Partitioning option
JServer Release 9.2.0.4.0 - Production
SQL> create table TBL_USER_PROFILE_CATEGORY
2 as
3 select 654 id_category, 1103 id_user from dual union all
4 select 654 id_category, 1104 id_user from dual union all
5 select 18 id_category, 1103 id_user from dual union all
6 select 629 id_category, 1103 id_user from dual union all
7 select 110 id_category, 1103 id_user from dual union all
8 select 110 id_category, 1104 id_user from dual union all
9 select 18 id_category, 1104 id_user from dual union all
10 select 37 id_category, 1103 id_user from dual union all
11 select 24 id_category, 1103 id_user from dual union all
12 select 7 id_category, 104 id_user from dual union all
13 select 37 id_category, 1104 id_user from dual union all
14 select 22 id_category, 1103 id_user from dual union all
15 select 22 id_category, 1104 id_user from dual union all
16 select 25 id_category, 1104 id_user from dual union all
17 select 25 id_category, 1103 id_user from dual ;
Table created.
SQL>
SQL> alter table TBL_USER_PROFILE_CATEGORY add primary key (id_category, id_use
Table altered.
SQL>
SQL> CREATE OR REPLACE
2 PROCEDURE P$UPDATE_TBL_USER_PROFILE_CAT(p_id_cat_old IN NUMBER) AS
3 p_id_category number;
4 p_id_user number;
5 cursor mycur is select id_category, id_user
6 from TBL_USER_PROFILE_CATEGORY
7 where id_category = p_id_cat_old
8 for update of id_category;
9 BEGIN
10 open mycur;
11 LOOP
12 FETCH mycur INTO p_id_category, p_id_user;
13 EXIT WHEN mycur%NOTFOUND;
14 BEGIN
15 DELETE FROM TBL_USER_PROFILE_CATEGORY
16 WHERE CURRENT OF mycur;
17 END;
18 END LOOP;
19 CLOSE mycur;
20 COMMIT;
21 EXCEPTION WHEN OTHERS THEN rollback;
22 END;
23 /
Procedure created.
SQL>
SQL> exec P$UPDATE_TBL_USER_PROFILE_CAT(654)
PL/SQL procedure successfully completed.
SQL> select * from TBL_USER_PROFILE_CATEGORY;
ID_CATEGORY ID_USER
18 1103
629 1103
110 1103
110 1104
18 1104
37 1103
24 1103
7 104
37 1104
22 1103
22 1104
25 1104
25 1103
13 rows selected.
SQL> Please, paste here what's your code...
Nicolas. -
Explain plan for query with variables
Trying to Explain plan for some sql in sql*plus. The query has a variable in it. How do I do this ?
I did look into explain plan and dbms_xplan but could not find anything with variablesuse sqlplus bind variables:
SQL> --define variable
SQL> var x varchar2
SQL> -- notice the colon prefixing the variable
SQL> explain plan for select * from customer where cid = :x;
Explained.
SQL> select * from table( dbms_xplan.display );
PLAN_TABLE_OUTPUT
Plan hash value: 1709312366
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 67 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| CUSTOMER | 1 | 67 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | CID | 1 | | 1 (0)| 00:00:01 |
Predicate Information (identified by operation id):
2 - access("CID"=:X)
14 rows selected.the variable does not need to be set to explain the query, since explain does not actually execute.
Edited by: shoblock on Nov 6, 2008 4:51 PM -
Hi,
I have a sql which is recently having a performance problems in Production. I have generated a explain plan for it trying to find out what it is doing but plan itself is close to 1000 lines. I want to check if there is any efficient way to go through big plan like this one and quickly find the damaging areas..
2) I also wanted to know if there is way to generate explain plans in HTML format which executed in past and have entry in dba_hist_sqltext.
3) I also have two sql_monitor reports which I want to compare. is there any efficient way to do it as well?
Please share your thoughts!
Thanks in advance!
Regards,
Suman-Hi,
I suggest you can try running sql advisor on the query maybe something fruitful comes up
http://www.oracle-base.com/articles/11g/sql-access-advisor-11gr1.php
I am not sure about the explain plan being printed in html format but
You may also want to try the sqlhistory.sql query from below page
http://evdbt.com/scripts/
I have used it many times to check on executions and explain plans which may have changed over the period
I have faced it many times , the query picks up a bad explain plan and performs poorly -
Dear All.
DBMS_XPLAN.DISPLAY_CURSOR(
sql_id IN VARCHAR2 DEFAULT NULL,
child_number IN NUMBER DEFAULT NULL,
format IN VARCHAR2 DEFAULT 'TYPICAL');
SQL> SELECT * FROM table (
2 DBMS_XPLAN.DISPLAY_CURSOR('b7jn4mf49n569'));
PLAN_TABLE_OUTPUT
SQL_ID b7jn4mf49n569, child number 0
select o.name, u.name from obj$ o, type$ t, user$ u where o.oid$ = t.tvoid and
u.user#=o.owner# and bitand(t.properties,8388608) = 8388608 and
(sysdate-o.ctime) > 0.0007
Plan hash value: 4266358741
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| T
| 0 | SELECT STATEMENT | | | | 94 (100)|
| 1 | NESTED LOOPS | | 1 | 72 | 94 (2)| 0
| 2 | NESTED LOOPS | | 1 | 56 | 93 (2)| 0
|* 3 | TABLE ACCESS FULL | OBJ$ | 71 | 2414 | 37 (3)| 0
|* 4 | TABLE ACCESS BY INDEX ROWID| TYPE$ | 1 | 22 | 1 (0)| 0
|* 5 | INDEX UNIQUE SCAN | I_TYPE2 | 1 | | 0 (0)|
| 6 | TABLE ACCESS CLUSTER | USER$ | 1 | 16 | 1 (0)| 0
|* 7 | INDEX UNIQUE SCAN | I_USER# | 1 | | 0 (0)|
PLAN_TABLE_OUTPUT
Predicate Information (identified by operation id):
3 - filter(("O"."OID$" IS NOT NULL AND SYSDATE@!-"O"."CTIME">.0007))
4 - filter(BITAND("T"."PROPERTIES",8388608)=8388608)
5 - access("O"."OID$"="T"."TVOID")
7 - access("U"."USER#"="O"."OWNER#")
29 rows selected
SQL> As you can see using DBMS_XPLAN.DISPLAY_CURSOR. I can display the explain plan of any query IN SQL*PLUS.
But I want to write a PL/SQL function that generating an explain plan for any query and displaying the explain plan in a htlm page
how can i do same thing in pl/sql? I need yours advice.
Thanks in advance!Generate the plan like so:
begin
execute immediate 'explain plan for select * from dual';
end;Then you can put the dbms_xplan.display bit into a ref cursor and pass that across to the front end.
Eg:
SQL> variable rc refcursor
SQL> begin
2 execute immediate 'explain plan for select * from dual';
3 open :rc for select * from table(dbms_xplan.display);
4* end;
SQL> /
PL/SQL procedure successfully completed.
SQL> print rc;
PLAN_TABLE_OUTPUT
Plan hash value: 3543395131
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 2 | 5 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| DUAL | 1 | 2 | 5 (0)| 00:00:01 |
8 rows selected. -
Explain plan for MVIEW....
Hi All,
please tell me how to invoke EXPLAIN PLAN for CREATE MATERIALIZED VIEW.
while i am trying to get plan i am getting following error
ORA-00900: invalid SQL statement
Thanks in advance.Thanks for reply.
I need it for estimation and solution design. so all
parameters are necessary. second thing is DDL and
simple SELECT statements plan are differ. I can get
explain plan for CREATE TABLE AS SELECT but not for
CREATE MV.
Regards,
Santosh.In case you didn't notice: Create table as select (CTAS) is also DDL.
There is nothing additionally from point of the execution plan that is added by the CREATE MV command. -
Running explain plan on a prepared statement
I'm using IBatis to run queries against an Oracle database. IBatis runs the queries as prepared statements. I want to run explain plan on the prepared statements.
I already have a shim -- a proxy java.sql.Connection object -- that takes the SQL used to create a prepared statement (e.g., "select a, b, c from table d where c = ?"), returns a proxy java.sql.PreparedStatement, and then on the PreparedStatement's execute(), interpolates the bound parameters into the SQL statement, and runs Explain Plan on that (e.g, preparedStatement.setInt(5) is called, followed by preparedStatement.execute(), which results in my proxy running "explain plan for select a, b, c from table d where c = 5;")
Using Spring configuration, I can transparently insert the shim and get explain plans for all queries run through that Connection.
The problem is this: I'm explaining the plan for the SQL with the bound parameters, not necessarily the SQL run for the prepared statement.
Assume that in the above example, there exists an index on column c in table d, and that 5% of d's rows have the value 1, 15% have the value 2, and 80% have the value 3.
For "select a, b, c from table d where c = 1", Oracle will use the index. But for "select a, b, c from table d where c = 3", Oracle will do a full table scan.
What I want to know is what Oracle will do for "select a, b, c from table d where c = ?" when "?" is an unknown, arbitrary value subsequently bound to 1 or to 3.
As I understand it, the query plan for a prepared statement is done once, when the prepared statement is created. At execute time (as I understand it), the query plan is not reformulated based on the actual bound values (though partition pruning is recalculated).
So how can I find out what query plan is actually be used for prepred statement?
Thanks.I already have a shim -- a proxy java.sql.Connection object -- that takes the SQL used to create a prepared statement I believe explain plan is part of PL/SQL rather than external to it.
As such you can pass it via a prepared statement. It is just a matter of getting the syntax correct. And of course collecting the result.
The syntax is going to be be something like the following. You would use it just like you use the existing sql.
begin
select ... from ... where c = ?;
endThe result might be returned a result sets (plural). If so you would need to code appropriately for that. -
Hi,
For getting the explain plan for a query, we use the statement
"explain plan for " + Query
Similarly, to get an explain plan for a procedure, do we have any way like
"explain plan for " + "Execute " + Procedure
How do we get an explain plan for a procedure that is executed
Thanks in Advance.teckfreak wrote:
Hi Robert,
I am working on an utility application which will execute the procedure and show the explain plan to the user for him to analyze the explain plan and take necessary steps for optimization. I am showing the Procedure inputs to the user, allowing him to enter values and taking them and executing the procedure. I am setting the trace and extracting the explain plan for the procedure using TKPROF utility and showing it to the user.
While doing so, the trace file is stored in udump folder. I am accessing the trace file from TKPROF utility. I am able to run the TKPROF from the command prompt. But if I try automating the TKPROF command in a .NET application using Process.Start, it says "Not able to access the file.". I tried giving full permissions to Everyone and it still throws the error. Kindly guide me how to proceed.. A different approach that you might want to consider (as indicated by William):
- Flush the shared pool and use a unique MODULE description for each execution of your procedure (e.g. using DBMS_APPLICATION_INFO.SET_MODULE), e.g. using a logon trigger
- Query V$SQL for your unique MODULE description and run DBMS_XPLAN.DISPLAY_CURSOR for each corresponding child cursor found (SQL_ID + CHILD_NUMBER) in the shared pool. This plan generation could be automated using a procedure
The result of this approach corresponds to the tracing using TKPROF since it will provide the actual execution plans used at runtime rather than separate EXPLAIN PLAN results which might differ from the actual plans.
This assumes that your shared pool is sufficiently large to hold all the child cursor created by your procedure without aging them out while the procedure is running. It's probably also only applicable to an environment where not too much work is being done while running this test and the recommended flushing of the shared pool.
Regards,
Randolf
Oracle related stuff blog:
http://oracle-randolf.blogspot.com/
SQLTools++ for Oracle (Open source Oracle GUI for Windows):
http://www.sqltools-plusplus.org:7676/
http://sourceforge.net/projects/sqlt-pp/ -
Explain plan for running query
Hi everyone,
I come to know how to generate explain plan for a given query by giving
Explain plan for select * fro emp;
Consider a query running for 5 hrs in a session and i want to genrate explain plan for that current query in its 4th hour i dont know the sql as well
all the steps by step would be much apppreciated
like finding Current SQL then so on
Thanks
Shareef912856 wrote:
Hi everyone,
I come to know how to generate explain plan for a given query by giving
Explain plan for select * fro emp;
Consider a query running for 5 hrs in a session and i want to genrate explain plan for that current query in its 4th hour i dont know the sql as well
all the steps by step would be much apppreciated
like finding Current SQL then so on
Thanks
ShareefYOu can also use dbms_xplain to generate plan used in v$sql. like for example
SQL>SELECT SQL_ID, CHILD_NUMBER FROM V$SQL WHERE SQL_TEXT LIKE 'select * from em%';
SQL_ID CHILD_NUMBER
6kd5fkqdjb8fu 0
SQL>SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('6kd5fkqdjb8fu',0,'ALLSTATS'));If you need the actual tuntime statistics used by sql statement then you need to put hint /*+ gather_plan_statistics */ in sql ststement, something like
select /*+ gather_plan_statistics */ * from emp;
and then generate the explain plan for this
Have a look
http://hoopercharles.wordpress.com/2010/03/01/dbms_xplan-format-parameters/
select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST')); -
How to run Explain Plan for SP?
Can we run Explain Plan for SP??
EXPLAIN PLAN creates an execution plan for SQL statements. PL/SQL is not SQL.
PL/SQL procedures can contain SQL, yes. These SQLs can be copied and pasted from PL/SQL into something like TOAD and execution plan determined.
However, if the aim is to see the "execution/performance plan" for PL/SQL code, then you need to profile the PL/SQL code using the DBMS_PROFILER package. This will tell you the execution path and elapsed time to execute a line of PL/SQL.
Details can be found in the [url http://download-east.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_profil.htm#sthref5502]Oracle® Database PL/SQL Packages and Types Reference guide. -
How to find Explain Plan for a large querry which has multiple sub querries
Hi All,
I have a Package which has many procedures and one of the procedure has a cursor which is like 2000 lines. This cursor has a main select statement which again has many select statements in it.
Now how do I do the explain plan for the main select statement?
If it can be done easier way in toad(or SQLPLUS), please tell me...When your query takes too long ...
-
Different 'execution plans' for same sql in 10R2
DB=10.2.0.5
OS=RHEL 3
Im not sure of this, but seeing different plans for same SQL.
select sql_text from v$sqlarea where sql_id='92mb4z83fg4st'; <---TOP SQL from AWR
SELECT /*+ OPAQUE_TRANSFORM */ "ENDUSERID","LASTLOGINATTEMPTTIMESTAMP","LOGINSOURCECD","LOGINSUCCESSFLG",
"ENDUSERLOGINATTEMPTHISTORYID","VERSION_NUM","CREATEDATE"
FROM "BOMB"."ENDUSERLOGINATTEMPTHISTORY" "ENDUSERLOGINATTEMPTHISTORY";
SQL> set autotrace traceonly
SQL> SELECT /*+ OPAQUE_TRANSFORM */ "ENDUSERID","LASTLOGINATTEMPTTIMESTAMP","LOGINSOURCECD","LOGINSUCCESSFLG",
"ENDUSERLOGINATTEMPTHISTORYID","VERSION_NUM","CREATEDATE"
FROM "BOMB"."ENDUSERLOGINATTEMPTHISTORY" "ENDUSERLOGINATTEMPTHISTORY"; 2 3
1822203 rows selected.
Execution Plan
Plan hash value: 568996432
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1803K| 75M| 2919 (2)| 00:00:36 |
| 1 | TABLE ACCESS FULL| ENDUSERLOGINATTEMPTHISTORY | 1803K| 75M| 2919 (2)| 00:00:36 |
Statistics
0 recursive calls
0 db block gets
133793 consistent gets
0 physical reads
0 redo size
76637183 bytes sent via SQL*Net to client
1336772 bytes received via SQL*Net from client
121482 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1822203 rows processed
===================================== another plan ===============
SQL> select * from TABLE(dbms_xplan.display_awr('92mb4z83fg4st'));
15 rows selected.
Execution Plan
Plan hash value: 3015018810
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | COLLECTION ITERATOR PICKLER FETCH| DISPLAY_AWR |
Note
- rule based optimizer used (consider using cbo)
Statistics
24 recursive calls
24 db block gets
49 consistent gets
0 physical reads
0 redo size
1529 bytes sent via SQL*Net to client
492 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
15 rows processed
=========second one shows only 15 rows...
Which one is correct ?Understood, second plan is for self 'dbms_xplan'.
Anyhow I opened a new session where I did NOT on 'auto-trace'. but plan is somewhat than the original.
SQL> /
PLAN_TABLE_OUTPUT
SQL_ID 92mb4z83fg4st
SELECT /*+ OPAQUE_TRANSFORM */ "ENDUSERID","LASTLOGINATTEMPTTIMESTAMP","LOGINSOURCECD","
LOGINSUCCESSFLG","ENDUSERLOGINATTEMPTHISTORYID","VERSION_NUM","CREATEDATE" FROM
"BOMB"."ENDUSERLOGINATTEMPTHISTORY" "ENDUSERLOGINATTEMPTHISTORY"
Plan hash value: 568996432
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
PLAN_TABLE_OUTPUT
| 0 | SELECT STATEMENT | | | | 2919 (100)| |
| 1 | TABLE ACCESS FULL| ENDUSERLOGINATTEMPTHISTORY | 1803K| 75M| 2919 (2)| 00:00:36 |
15 rows selected.
I am just wondering, which plan is the accurate and which I need to believe ?
Maybe you are looking for
-
Looking for a specific document
I cant find a certain document on the portal. Its called the Post Go-live Questionaire. This document is to do a Post Go-Live System Audit and to document the results and possible issues of the initial SBO Go-Live. Do anyone maybe have a link to this
-
Cisco VPN no longer works with the E4200
VPN issue with the E4200. Validated that VPN passthrough is enabled. Using Cisco VPN as the client. Used to work prior to the firmware upgrade so not sure what has changed. Error code on the Cisco VPN client is "Secure VPN connection terminated b
-
Can't sync songs after 14 gb, itunes gives errors!! Please help!
Hello to everyone, I'm using an Ipod classic, 6th generations, 2009 and i am using itunes with windows 8.1. After the new update of Itunes, i couldn't sync new songs to my Ipod, Itunes shows a lot of erros like 50, 53, 13039. I read and tried a lot o
-
How to access variable values across mappings?
Hi all, I have created the following sample class Test.java and imported it as an archive: public class Test{ public static String myStringData = null; In one of the mappings, I have a UDF that sets the "myStringData" using the foll: Test.myStringDa
-
Design Studio working for ipad,iphone, and desktop
Hello All, I'm new to the SAP design studio world. My first task that has been assigned to me is to see what is the capability of Design Studio to have a singular file and have the CSS be reflective of Iphone,Ipad and Desktop. 1) Is this even possibl