Dynamic query plan vs normal query plan

I have a query with like operator.
DECLARE @query varchar(52)
SET @query='A12657'
IF @query IS NOT NULL
SET @query='%'+LTRIM(RTRIM(@query))+'%'SELECT eord_type_id FROM izdelek
WHERE (izd_naziv_ANG like @query OR izd_id like @query)
OPTION (RECOMPILE)
Query works 1 ms, 5 ms is for plan recompile. The execution plan is:
But if I run the same query as dynamic sql:
DECLARE @query varchar(52), @sql NVARCHAR(4000), @paramList NVARCHAR(500)
SET @query='A12657'
SELECT @paramlist = N'@query VARCHAR(52)'
if @query IS NOT NULL
SET @query='%'+LTRIM(RTRIM(@query))+'%'
SET @sql=N'SELECT eord_type_id FROM izdelek
WHERE (izd_naziv_ANG like @query OR izd_id like @query)
OPTION (RECOMPILE)'
EXEC sp_executesql @sql, @paramlist, @query
The execution plan is different and execution time is much slower - 2877 ms.
Where is the catch? What should I change that dynamic query would work the same.
What I also don't understand is key lookup at the end of the plan. Since we have clustered index seek at the beginning of the plan, the optimizer could read additional column(eord_type_id) from this index seek.
And why it is using merge since it has both columns from WHERE clause (izd_naziv_ang and izd_id) included inside index IX_izdelek. izd_id is in fact clustered key.
How can i improve this query?
But I would like to know at most, why dynamic sql is so much slower, since the query is totally the same in both cases and how to achieve that both queries have the similar execution time?

First of all , why do you use LIKE operator instead of EQUAL (=). I see you are looking for exact value..
--SQL Server creates 3 execution plan rather only one
DBCC FREEPROCCACHE
GO
SELECT *
FROM Sales.SalesOrderHeader
WHERE SalesOrderID = 56000
GO
SELECT * FROM
AdventureWorks.Sales.SalesOrderHeader WHERE
SalesOrderID = 56001
GO
declare @i int
set @i = 56004
SELECT *
FROM Sales.SalesOrderHeader
WHERE SalesOrderID = @i
GO
select  stats.execution_count AS exec_count, 
p.size_in_bytes as [size], 
[sql].[text] as [plan_text]
from sys.dm_exec_cached_plans p
outer apply sys.dm_exec_sql_text (p.plan_handle) sql
join sys.dm_exec_query_stats stats ON stats.plan_handle = p.plan_handle
GO
----This time only (we get parameterization)
DBCC FREEPROCCACHE
GO
EXEC sp_executesql N'SELECT  SUM(LineTotal) AS LineTotal
FROM Sales.SalesOrderHeader H
JOIN Sales.SalesOrderDetail D ON D.SalesOrderID = H.SalesOrderID
WHERE H.SalesOrderID = @SalesOrderID', N'@SalesOrderID INT', 56000
GO
EXEC sp_executesql N'SELECT  SUM(LineTotal) AS LineTotal
FROM Sales.SalesOrderHeader H
JOIN Sales.SalesOrderDetail D ON D.SalesOrderID = H.SalesOrderID
WHERE H.SalesOrderID = @SalesOrderID', N'@SalesOrderID INT', 56005
GO
select  stats.execution_count AS exec_count, 
LEFT([sql].[text], 80) as [plan_text]
from sys.dm_exec_cached_plans p
outer apply sys.dm_exec_sql_text (p.plan_handle) sql
join sys.dm_exec_query_stats stats ON stats.plan_handle = p.plan_handle
GO
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence

Similar Messages

  • LEFT JOINT OUTER  has diferent plan from normal query

    Hi All
    I have two table TRANS and TRANS_PARTY, on trans table has index on LAST_MODIFIED and on trans_party has index on transid
    I have two queries. They are the same about meaning but Oracle has different plan.
    Can anyone explain the reason why?
    SQL> explain plan for select count(*) from trans t1t
    2 LEFT OUTER JOIN trans_party t1tp ON
    (t1t.id=t1tp.transid AND t1t.last_modified >=to_date('2009-09-04 00:00:00','YYYY-MM-DD HH24:MI:SS')
    AND t1t.last_modified <to_date('2009-09-05 00:00:00','YYYY-MM-DD HH24:MI:SS')
    and t1tp.last_modified >=to_date('2009-09-04 00:00:00','YYYY-MM-DD HH24:MI:SS')
    AND t1tp.last_modified <to_date('2009-09-05 00:00:00','YYYY-MM-DD HH24:MI:SS'))
    3 4 5 6 7 ;
    Explained.
    SQL>
    SQL> set pagesize 999;
    set linesize 999;SQL>
    SQL>
    SQL> SELECT * FROM TABLE(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 2040563577
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1 | 17 | 275K (1)| 00:55:07 |
    | 1 | SORT AGGREGATE | | 1 | 17 | | |
    | 2 | NESTED LOOPS OUTER | | 1233K| 19M| 275K (1)| 00:55:07 |
    | 3 | TABLE ACCESS FULL | TRANS | 1233K| 19M| 4085 (3)| 00:00:50 |
    | 4 | VIEW | | 1 | | 0 (0)| 00:00:01 |
    |* 5 | FILTER | | | | | |
    |* 6 | TABLE ACCESS BY INDEX ROWID| TRANS_PARTY | 1 | 17 | 1 (0)| 00:00:01 |
    |* 7 | INDEX RANGE SCAN | IXTPFK | 1 | | 1 (0)| 00:00:01 |
    Predicate Information (identified by operation id):
    5 - filter("T1T"."LAST_MODIFIED"<TIMESTAMP'2009-09-05 00:00:00' AND
    "T1T"."LAST_MODIFIED">=TIMESTAMP'2009-09-04 00:00:00')
    6 - filter("T1TP"."LAST_MODIFIED">=TIMESTAMP'2009-09-04 00:00:00' AND
    "T1TP"."LAST_MODIFIED"<TIMESTAMP'2009-09-05 00:00:00')
    7 - access("T1T"."ID"="T1TP"."TRANSID")
    23 rows selected.
    SQL> explain plan for select count(*) from trans t , trans_party tp
    where t.id(+)=tp.transid
    and t.last_modified >=to_date('2009-09-04 00:00:00','YYYY-MM-DD HH24:MI:SS')
    AND t.last_modified <to_date('2009-09-05 00:00:00','YYYY-MM-DD HH24:MI:SS')
    and (tp.agentid=2654492 OR tp.agentid=2654492) 2 3 4 5 ;
    Explained.
    SQL> SELECT * FROM TABLE(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 1553247449
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
    | 0 | SELECT STATEMENT | | 1 | 31 | 3 (0)| 00:00:01 | | |
    | 1 | SORT AGGREGATE | | 1 | 31 | | | | |
    |* 2 | TABLE ACCESS BY LOCAL INDEX ROWID | TRANS_PARTY | 1 | 13 | 1 (0)| 00:00:01 | | |
    | 3 | NESTED LOOPS | | 1 | 31 | 3 (0)| 00:00:01 | | |
    | 4 | PARTITION RANGE ALL | | 1 | 18 | 1 (0)| 00:00:01 | 1 | 71 |
    | 5 | TABLE ACCESS BY LOCAL INDEX ROWID| TRANS | 1 | 18 | 1 (0)| 00:00:01 | 1 | 71 |
    |* 6 | INDEX RANGE SCAN | IXTRLASTMOD | 1 | | 1 (0)| 00:00:01 | 1 | 71 |
    | 7 | PARTITION RANGE ALL | | 3 | | 1 (0)| 00:00:01 | 1 | 71 |
    |* 8 | INDEX RANGE SCAN | IXTPFK | 3 | | 1 (0)| 00:00:01 | 1 | 71 |
    Predicate Information (identified by operation id):
    2 - filter("TP"."AGENTID"=2654492)
    6 - access("T"."LAST_MODIFIED">=TIMESTAMP' 2009-09-04 00:00:00' AND "T"."LAST_MODIFIED"<TIMESTAMP'
    2009-09-05 00:00:00')
    8 - access("T"."ID"="TP"."TRANSID")
    23 rows selected.
    SQL>
    Thanks
    Edited by: user554265 on Sep 7, 2009 7:13 AM

    Toon Koppelaars wrote:
    My advice: stop using the ANSI SQL syntax for doing (outer) joins.
    It is confusing.
    It is hard to read.
    It is hard to understand.Hmmm... blatent statements and no proof.
    I'm adept at using either syntax.
    ANSI is not confusing to me, it's quite the opposite. Therefore your view that it's confusing is just that... your view.
    Hard to read? Actually, I find that it clarifies the join conditions from the clauses and makes outer joins easier to specify. Again, that's your view.
    Hard to understand? Nah, I can understand both just as easily. What's hard about it?
    Also bear in mind...
    Oracle syntax does not support outer joining to more than one table.
    However ANSI syntax does...
    SQL> select * from a;
            ID      B_KEY      C_KEY
             1          2          3
             2          1          4
             3          3          1
             4          4          2
    SQL> select * from b;
            ID     C_KEY2
             1          1
             2          5
             3          3
             4          2
    SQL> select * from c;
          KEY1       KEY2 DTA
             1          1 1-1
             1          2 1-2
             1          3 1-3
             1          4 1-4
             2          1 2-1
             2          2 2-2
             2          3 2-3
             2          4 2-4
             3          1 3-1
             3          2 3-2
             3          3 3-3
             3          4 3-4
             4          1 4-1
             4          2 4-2
             4          3 4-3
             4          4 4-4
    16 rows selected.
    SQL> ed
    Wrote file afiedt.buf
      1  select a.id as a_id, b.id as b_id, c.key1 as c_key1, c.key2 as c_key3, c.dta
      2  from a, b, c
      3  where a.b_key = b.id
      4  and   a.c_key = c.key1 (+)
      5* and   b.c_key2 = c.key2 (+)
    SQL> /
    and   a.c_key = c.key1 (+)
    ERROR at line 4:
    ORA-01417: a table may be outer joined to at most one other table
    SQL> ed
    Wrote file afiedt.buf
      1  select a.id as a_id, b.id as b_id, c.key1 as c_key1, c.key2 as c_key3, c.dta
      2  from a JOIN b ON (a.b_key = b.id)
      3*        LEFT OUTER JOIN c ON (a.c_key = c.key1 and b.c_key2 = c.key2)
    SQL> /
          A_ID       B_ID     C_KEY1     C_KEY3 DTA
             3          3          1          3 1-3
             4          4          2          2 2-2
             2          1          4          1 4-1
             1          2
    SQL>;)

  • Replace joins with normal query

    hi experts ,
    can u help me out splving in this issue.
    i want to convert this query in to normal query I.e  with out usin gany joins .pls help me out.
    select a~ebeln
            b~ebelp
            b~loekz
            a~bukrs
            a~wkurs
            a~lifnr
            a~ekorg
            a~ekgrp
            a~waers
            a~bedat
            b~txz01
            b~menge
            b~meins
            b~netpr
            b~elikz
            b~erekz
            b~wepos
            b~weunb
            b~repos
            b~webre
            b~matkl
            c~eindt
       from ekko as a
       inner join ekpo as b
       on aebeln eq bebeln
    inner join eket as c
    on  bebeln eq cebeln
    and bebelp eq cebelp
    into corresponding fields of table t_ekko_ekpo
    where a~bukrs in s_bukrs
      and lifnr   in s_lifnr
      and ekorg   in s_ekorg
      and ekgrp   in s_ekgrp
      and a~bedat in s_bedat.
      and c~eindt in s_eindt.
    Thanks in advance ,

    Hi all,
    Can someone tell me if it is possible to replace a
    call like
    <jbo:ShowValue datasource="dsNews"
    dataitem="NisStory" /> with
    String s = jbo.ShowValue(dsNews,NisStory)? Or if there
    is another way to get the output of the tag into a
    String?One way to do it would be to wrap the execution of <jbo:ShowValue/> with a body tag that captures its body and writes it to a variable in the pageContext. The usage would be something like:
    <mytaglib:capture var="aString">
    <jbo:ShowValue/>
    </mytaglib:capture>
    the capture tag handler would take the body content in the doAfterBody callback and expose it as "aString" in the pageContext attributes.
    >
    Thanks in advance.
    Regards BasBr - J

  • Fix a plan of the query

    Hi,
    I want to set execution plan of a query. This query is dynamically created by the application according to IN parameters. Actually the query is not using proper index.
    Can we use outline. If yes then please give me the syntax for the same.
    Please suggest what to do in the situation.
    Thanks

    Yes.
    SQL_TEXT
    select /*+ first_rows */ nvl(lad.DBID,1) dbid, applid,lesseeid,
    crdec, lad.Instlmode,lad.branchid nbfc_branchid,ldt.INST_PD_FULL
    ,ncm.customerid,customername, nvl(lad.proposalmodby,lad.propos
    alcrby) proposalmodby,lad.status lea_status,Trim(to_char(lad.amt
    fin,'999,999,999,999,999,990.90')) amtfin, lad.emi emi,lad.dpd d
    pd,lad.bucket bucket,lad.npa_stageid npa_stageid, lad.schemeid,p
    roductflag from lea_agreement_dtl lad, nbfc_customer_m ncm, LOAN
    DETAILtable ldt Where ldt.AgreementID(+) = lad.AgreementID and
    lad.lesseeid=ncm.customerid and lad.STATUS = :V00001 and lad.b
    ranchid = :V00002 and lad.productflag = :V00003 and lad.FIRSTSOU
    RCEID = :V00004 and lad.FIRSTSOURCE = :V00005 and lad.AUTHDATE b
    etween TO_DATE(:V00006,'DD/MM/RRRR') and TO_DATE(:V00007,'DD/MM
    /RRRR') and rownum <= 50 order by applid desc
    OPERATION      OPTIONS     OBJECT_NAME     COST     Bytes
    SELECT STATEMENT               9     
    COUNT      STOPKEY               
    NESTED LOOPS                9     104
    NESTED LOOPS      OUTER          7     82
    TABLE ACCESS     BY INDEX ROWID     LEA_AGREEMENT_DTL     5     73
    INDEX      RANGE SCAN     LEA_AGREEMENT_DTL_IDX1     4     
    TABLE ACCESS     BY INDEX ROWID     LOAN_DETAIL_TABLE     3     9
    INDEX      UNIQUE SCAN     SYS_C0021211     2     
    TABLE ACCESS      BY INDEX ROWID     NBFC_CUSTOMER_M     3     22
    INDEX      UNIQUE SCAN     NBFC_CUSTOMER_M_PK     2
    Now we want to use CR_DECISION_M_AUTHDATE_IDX1 index in place of LEA_AGREEMENT_DTL_IDX1. Bcz CR_DECISION_M_AUTHDATE_IDX1 has AUTHDATE column in it and is faster than other index.
    Thanks
    Ratan

  • 'dbms_xplan.display_awr' is showing two plan for single query

    Hi,
    I am trying to fetch sql plan from awr, but it's showing two different plans for a single query:
    PLAN_TABLE_OUTPUT
    SQL_ID 2pxv33cr271sb
    SELECT P_DEP_BNK_CODE,P_DEP_BRN_CODE,P_DEP_DATE,
                                   P_DEP_DAY_SL,P_DEP_INST_SL,P_INST_AMT                                       
                             FROM P WHERE
                                        P_DEP_BNK_CODE = :1 AND
                                    P_DEP_BRN_CODE = :2 AND
                                P_DEP_DATE     = :3 AND
                            P_DEP_LCC_UCC  = :4 AND
                        P_DEP_DAY_SL   = :5 AND
                    (P_REALISED_ON IS NULL AND
                    P_RTN_DATE IS NULL)
    Plan hash value: 3064382432
    | Id  | Operation                          | Name      | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
    |   0 | SELECT STATEMENT                   |           |       |       |     5 (100)|          |       |       |
    |   1 |  TABLE ACCESS BY GLOBAL INDEX ROWID| P         |     1 |    40 |     5   (0)| 00:00:01 | ROWID | ROWID |
    |   2 |   INDEX RANGE SCAN                 | P_PK      |     1 |       |     4   (0)| 00:00:01 |       |       |
    Note
       - dynamic sampling used for this statement (level=5)
    SQL_ID 2pxv33cr271sb
    SELECT P_DEP_BNK_CODE,P_DEP_BRN_CODE,P_DEP_DATE,
                                   P_DEP_DAY_SL,P_DEP_INST_SL,P_INST_AMT                                       
                             FROM P WHERE
                                        P_DEP_BNK_CODE = :1 AND
                                    P_DEP_BRN_CODE = :2 AND
                                P_DEP_DATE     = :3 AND
                            P_DEP_LCC_UCC  = :4 AND
                        P_DEP_DAY_SL   = :5 AND
                    (P_REALISED_ON IS NULL AND
                    P_RTN_DATE IS NULL)
    Plan hash value: 3447007225
    | Id  | Operation            | Name     | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |    TQ  |IN-OUT| PQ Distrib |
    |   0 | SELECT STATEMENT     |          |       |       |     7 (100)|          |       |       |        |      |            |
    |   1 |  PX COORDINATOR      |          |       |       |            |          |       |       |        |      |            |
    |   2 |   PX SEND QC (RANDOM)| :TQ10000 |     1 |    38 |     7  (43)| 00:00:01 |       |       |  Q1,00 | P->S | QC (RAND)  |
    |   3 |    PX BLOCK ITERATOR |          |     1 |    38 |     7  (43)| 00:00:01 |   KEY |   KEY |  Q1,00 | PCWC |            |
    |   4 |     TABLE ACCESS FULL| P        |     1 |    38 |     7  (43)| 00:00:01 |   KEY |   KEY |  Q1,00 | PCWP |            |
    ------------------------------------------------------------------------------------------------------------------------------The database version is 11.2.0.1 and the underlying table is partitioned. Why this is showing two plans? Although the plan doesn't look expensive but this is causing maximum gets and enq: row lock contention
    Regards,
    Regards

    SQL> set autot on
    SQL> SELECT
      2  SUM(NVL(P_INST_AMT, 0))
      3  FROM
      4  AXISCMS.P
      5  WHERE
      6  P_DEP_BNK_CODE = '211'
      7  AND
      8  P_DEP_BRN_CODE = '005'
      9  AND
    10  P_DEP_DATE = to_date('11-NOV-2010')
    11  AND
    12  P_DEP_LCC_UCC = 'L'
    13  AND
    P_DEP_DAY_SL = 15001
    14   15  AND
    16  (P_REALISED_ON IS NOT NULL OR P_RTN_DATE IS NOT NULL );
    SUM(NVL(P_INST_AMT,0))
    Execution Plan
    | Id  | Operation                           | Name      | Rows  | Bytes | Cost  | Pstart| Pstop |
    |   0 | SELECT STATEMENT                    |           |     1 |    38 |     8 |       |       |
    |   1 |  SORT AGGREGATE                     |           |     1 |    38 |       |       |       |
    |   2 |   TABLE ACCESS BY GLOBAL INDEX ROWID| P          |     1 |    38 |     8 |    72 |    72 |
    |   3 |    INDEX RANGE SCAN                 | P_PK      |     1 |       |     4 |       |       |
    Note
       - 'PLAN_TABLE' is old version
    Statistics
              5  recursive calls
              0  db block gets
            370  consistent gets
             31  physical reads
              0  redo size
            543  bytes sent via SQL*Net to client
            524  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
              1  rows processedThe above query seems to be having cheapest cost but causing a lot of slowness in the database.
    Column Description:
    COLUMN_NAME                    DATA_TYPE                      NUM_DISTINCT  NUM_NULLS    DENSITY HISTOGRAM
    P_AC_NAME                 VARCHAR2                             244672   49251591 4.0871E-06 NONE
    P_AC_NO                   VARCHAR2                             413792   48713721 2.4167E-06 NONE
    P_ARNGMNT_CREDIT_DATE     DATE                                   1708   39366743 .001724138 HEIGHT BALANCED
    P_AUTOLIQ_ACT_DATE        DATE                                    358   50356077 .003003003 HEIGHT BALANCED
    P_AUTOLIQ_DUE_DATE        DATE                                    337   50427426 .002967359 NONE
    P_AUTOLIQ_FLAG            CHAR                                      1   44746714          1 NONE
    P_CLG_DNLD_FLAG           NUMBER                                    1   50481119 .035714286 FREQUENCY
    P_CORR_BANK_REAL_DATE     DATE                                      0   50481133          0 NONE
    P_DEP_BNK_CODE            VARCHAR2                                 16          0 9.9454E-09 FREQUENCY
    P_DEP_BRN_CODE            VARCHAR2                                671          0 .002531646 HEIGHT BALANCEDIndex Details
    INDEX_NAME                     COLUMN_NAME                    COLUMN_POSITION
        P_IDX_FULL                P_INST_NO                               1
        P_IDX_FULL                P_DRAWN_ON_BANK                         2
        P_IDX_FULL                P_DRAWN_ON_BRN                          3
        P_IDX_FULL                P_INST_TYPE                             4
        P_IDX_FULL                P_DRAWN_ON_LOC                          5
        P_IDX_FULL                P_RTN_DATE                              6
        P_INDX1                   P_INST_NO                               1
        P_INDX2                   P_RTN_DATE                              1
        P_INDX2                   P_RTN_DAY_SL                            2
        P_INDX2                   P_RTN_INS_SL                            3
        P_INDX3                   P_REALISED_ON                           1
        P_INDX3                   P_REALISED_DAY_SL                       2
        P_INDX3                   P_REALISED_INS_SL                       3
        P_INDX4                   P_REV_TO_COL_DUE_DATE                   1
        P_INDX4                   P_REV_TO_COL_DONE_ON                    2
        P_INDX5                   P_ARNGMNT_CREDIT_DATE                   1
        P_INDX6                   P_POOL_POST_DATE                        1
        P_INDX6                   P_POOL_POST_DAYSL                       2
        P_PK                      P_DEP_BNK_CODE                          1
        P_PK                      P_DEP_BRN_CODE                          2
        P_PK                      P_DEP_DATE                              3
        P_PK                      P_DEP_LCC_UCC                           4
        P_PK                      P_DEP_DAY_SL                            5
        P_PK                      P_DEP_INST_SL                           6
    Top 5 Timed Foreground Events
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                               Avg
                                                              wait   % DB
    Event                                 Waits     Time(s)   (ms)   time Wait Class
    DB CPU                                           58,454          13.1
    enq: TX - row lock contention         1,095      17,691  16156    4.0 Applicatio
    read by other session             1,719,661      11,392      7    2.6 User I/O
    latch: cache buffers chains         264,753      10,758     41    2.4 Concurrenc
    latch free                           78,456       8,215    105    1.8 Other
    The query comes on top in every section in AWR.Regards,

  • Effect of RLS policy (VPD) on execution plan of a query

    Hi
    I have been working on tuning of few queries. A RLS policy is defined on most of the tables which appends an extra where condition (something like AREA_CODE=1). I am not able to understand the effect of this extra where clause on the execution plan of the query. In the execution plan there is no mention of the clause added by VPD. In 10046 trace it does show the policy function being executed but nothing after that.
    Can someone shed some light on the issue that has VPD any effect on the execution plan of the query ? Also would it matter whether the column on which VPD is applied, was indexed or non-indexed ?
    Regards,
    Amardeep Sidhu

    Amardeep Sidhu wrote:
    I have been working on tuning of few queries. A RLS policy is defined on most of the tables which appends an extra where condition (something like AREA_CODE=1). I am not able to understand the effect of this extra where clause on the execution plan of the query. In the execution plan there is no mention of the clause added by VPD. In 10046 trace it does show the policy function being executed but nothing after that.
    VPD is supposed to be invisible - which is why you get minimal information about security predicates in the standard trace file. However, if you reference a table with a security preidcate in your query, the table is effectively replaced by an inline view of the form: "select * from original_table where {security_predicate}", and the result is then optimised. So the effects of the security predicate is just the same as you writing the predicate into the query.
    Apart from your use of v$sql_plan to show the change in plan and the new predicates, you can see the effects of the predicates by setting event 10730 with 10046. In current versions of Oracle this causes the substitute view being printed in the trace file.
    Bear in mind that security predicates can be very complex - including subqueries - so the effect isn't just that of including the selectivity of "another simple predicate".
    Can someone shed some light on the issue that has VPD any effect on the execution plan of the query ? Also would it matter whether the column on which VPD is applied, was indexed or non-indexed ?
    Think of the effect of changing the SQL by hand - and how you would need to optimise the resultant query. Sometimes you do need to modify your indexing to help the security predicates, sometimes it won't make enough difference to matter.
    Regards
    Jonathan Lewis
    http://jonathanlewis.wordpress.com
    http://www.jlcomp.demon.co.uk
    "Science is more than a body of knowledge; it is a way of thinking"
    Carl Sagan
    To post code, statspack/AWR report, execution plans or trace files, start and end the section with the tag {noformat}{noformat} (lowercase, curly brackets, no spaces) so that the text appears in fixed format.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • How to improve the query performance or tune query from Explain Plan

    Hi
    The following is my explain plan for sql query. (The plan is generated by Toad v9.7). How to fix the query?
    SELECT STATEMENT ALL_ROWSCost: 4,160 Bytes: 25,296 Cardinality: 204                                         
         8 NESTED LOOPS Cost: 3 Bytes: 54 Cardinality: 1                                    
              5 NESTED LOOPS Cost: 2 Bytes: 23 Cardinality: 1                               
                   2 TABLE ACCESS BY INDEX ROWID TABLE AR.RA_CUSTOMER_TRX_ALL Cost: 1 Bytes: 13 Cardinality: 1                          
                        1 INDEX UNIQUE SCAN INDEX (UNIQUE) AR.RA_CUSTOMER_TRX_U1 Cost: 1 Cardinality: 1                     
                   4 TABLE ACCESS BY INDEX ROWID TABLE AR.HZ_CUST_ACCOUNTS Cost: 1 Bytes: 10 Cardinality: 1                          
                        3 INDEX UNIQUE SCAN INDEX (UNIQUE) AR.HZ_CUST_ACCOUNTS_U1 Cost: 1 Cardinality: 1                     
              7 TABLE ACCESS BY INDEX ROWID TABLE AR.HZ_PARTIES Cost: 1 Bytes: 31 Cardinality: 1                               
                   6 INDEX UNIQUE SCAN INDEX (UNIQUE) AR.HZ_PARTIES_U1 Cost: 1 Cardinality: 1                          
         10 TABLE ACCESS BY INDEX ROWID TABLE AR.RA_CUSTOMER_TRX_ALL Cost: 1 Bytes: 12 Cardinality: 1                                    
              9 INDEX UNIQUE SCAN INDEX (UNIQUE) AR.RA_CUSTOMER_TRX_U1 Cost: 1 Cardinality: 1                               
         15 NESTED LOOPS Cost: 2 Bytes: 29 Cardinality: 1                                    
              12 TABLE ACCESS BY INDEX ROWID TABLE AR.RA_CUSTOMER_TRX_ALL Cost: 1 Bytes: 12 Cardinality: 1                               
                   11 INDEX UNIQUE SCAN INDEX (UNIQUE) AR.RA_CUSTOMER_TRX_U1 Cost: 1 Cardinality: 1                          
              14 TABLE ACCESS BY INDEX ROWID TABLE ONT.OE_ORDER_HEADERS_ALL Cost: 1 Bytes: 17 Cardinality: 1                               
                   13 INDEX RANGE SCAN INDEX (UNIQUE) ONT.OE_ORDER_HEADERS_U2 Cost: 1 Cardinality: 1                          
         21 FILTER                                    
              16 TABLE ACCESS FULL TABLE ONT.OE_TRANSACTION_TYPES_TL Cost: 2 Bytes: 1,127 Cardinality: 49                               
              20 NESTED LOOPS Cost: 2 Bytes: 21 Cardinality: 1                               
                   18 TABLE ACCESS BY INDEX ROWID TABLE AR.RA_CUSTOMER_TRX_ALL Cost: 1 Bytes: 12 Cardinality: 1                          
                        17 INDEX UNIQUE SCAN INDEX (UNIQUE) AR.RA_CUSTOMER_TRX_U1 Cost: 1 Cardinality: 1                     
                   19 INDEX RANGE SCAN INDEX (UNIQUE) ONT.OE_ORDER_HEADERS_U2 Cost: 1 Bytes: 9 Cardinality: 1                          
         23 TABLE ACCESS BY INDEX ROWID TABLE AR.RA_CUSTOMER_TRX_ALL Cost: 1 Bytes: 12 Cardinality: 1                                    
              22 INDEX UNIQUE SCAN INDEX (UNIQUE) AR.RA_CUSTOMER_TRX_U1 Cost: 1 Cardinality: 1                               
         45 NESTED LOOPS Cost: 4,160 Bytes: 25,296 Cardinality: 204                                    
              42 NESTED LOOPS Cost: 4,150 Bytes: 23,052 Cardinality: 204                               
                   38 NESTED LOOPS Cost: 4,140 Bytes: 19,992 Cardinality: 204                          
                        34 NESTED LOOPS Cost: 4,094 Bytes: 75,850 Cardinality: 925                     
                             30 NESTED LOOPS Cost: 3,909 Bytes: 210,843 Cardinality: 3,699                
                                  26 PARTITION LIST ALL Cost: 2,436 Bytes: 338,491 Cardinality: 14,717 Partition #: 29 Partitions accessed #1 - #18          
                                       25 TABLE ACCESS BY LOCAL INDEX ROWID TABLE XLA.XLA_AE_HEADERS Cost: 2,436 Bytes: 338,491 Cardinality: 14,717 Partition #: 29 Partitions accessed #1 - #18     
                                            24 INDEX SKIP SCAN INDEX XLA.XLA_AE_HEADERS_N1 Cost: 264 Cardinality: 1,398,115 Partition #: 29 Partitions accessed #1 - #18
                                  29 PARTITION LIST ITERATOR Cost: 1 Bytes: 34 Cardinality: 1 Partition #: 32           
                                       28 TABLE ACCESS BY LOCAL INDEX ROWID TABLE XLA.XLA_AE_LINES Cost: 1 Bytes: 34 Cardinality: 1 Partition #: 32      
                                            27 INDEX RANGE SCAN INDEX (UNIQUE) XLA.XLA_AE_LINES_U1 Cost: 1 Cardinality: 1 Partition #: 32
                             33 PARTITION LIST ITERATOR Cost: 1 Bytes: 25 Cardinality: 1 Partition #: 35                
                                  32 TABLE ACCESS BY LOCAL INDEX ROWID TABLE XLA.XLA_DISTRIBUTION_LINKS Cost: 1 Bytes: 25 Cardinality: 1 Partition #: 35           
                                       31 INDEX RANGE SCAN INDEX XLA.XLA_DISTRIBUTION_LINKS_N3 Cost: 1 Cardinality: 1 Partition #: 35      
                        37 PARTITION LIST SINGLE Cost: 1 Bytes: 16 Cardinality: 1 Partition #: 38                     
                             36 TABLE ACCESS BY LOCAL INDEX ROWID TABLE XLA.XLA_EVENTS Cost: 1 Bytes: 16 Cardinality: 1 Partition #: 39 Partitions accessed #2               
                                  35 INDEX UNIQUE SCAN INDEX (UNIQUE) XLA.XLA_EVENTS_U1 Cost: 1 Cardinality: 1 Partition #: 40 Partitions accessed #2          
                   41 PARTITION LIST SINGLE Cost: 1 Bytes: 15 Cardinality: 1 Partition #: 41                          
                        40 TABLE ACCESS BY LOCAL INDEX ROWID TABLE XLA.XLA_TRANSACTION_ENTITIES Cost: 1 Bytes: 15 Cardinality: 1 Partition #: 42 Partitions accessed #2                    
                             39 INDEX UNIQUE SCAN INDEX (UNIQUE) XLA.XLA_TRANSACTION_ENTITIES_U1 Cost: 1 Cardinality: 1 Partition #: 43 Partitions accessed #2               
              44 TABLE ACCESS BY INDEX ROWID TABLE GL.GL_CODE_COMBINATIONS Cost: 1 Bytes: 11 Cardinality: 1                               
                   43 INDEX UNIQUE SCAN INDEX (UNIQUE) GL.GL_CODE_COMBINATIONS_U1 Cost: 1 Cardinality: 1

    damorgan wrote:
    Tuning is NOT about reducing the cost of i/o.
    i/o is only one of many contributors to cost and only one of many contributors to waits.
    Any time you would like to explore this further run this code:
    SELECT 1 FROM dual
    WHERE regexp_like(' ','^*[ ]*a');but not on a production box because you are going to experience an extreme tuning event with zero i/o.
    And when I say "extreme" I mean "EXTREME!"
    You've been warned.I think you just need a faster server.
    SQL> set autotrace traceonly statistics
    SQL> set timing on
    SQL> select 1 from dual
      2  where
      3  regexp_like   (' ','^*[ ]*a');
    no rows selected
    Elapsed: 00:00:00.00
    Statistics
              1  recursive calls
              0  db block gets
              0  consistent gets
              0  physical reads
              0  redo size
            243  bytes sent via SQL*Net to client
            349  bytes received via SQL*Net from client
              1  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
              0  rows processedRepeated from an Oracle 10.2.0.x instance:
    SQL> SELECT DISTINCT SID FROM V$MYSTAT;
           SID
           310
    SQL> ALTER SESSION SET EVENTS '10053 TRACE NAME CONTEXT FOREVER, LEVEL 1';
    Session altered.
    SQL> select 1 from dual
      2  where
      3  regexp_like   (' ','^*[ ]*a');The session is hung. Wait a little while and connect to the database using a different session:
    COLUMN STAT_NAME FORMAT A35 TRU
    SET PAGESIZE 200
    SELECT
      STAT_NAME,
      VALUE
    FROM
      V$SESS_TIME_MODEL
    WHERE
      SID=310;
    STAT_NAME                                VALUE
    DB time                                   9247
    DB CPU                                    9247
    background elapsed time                      0
    background cpu time                          0
    sequence load elapsed time                   0
    parse time elapsed                        6374
    hard parse elapsed time                   5997
    sql execute elapsed time                  2939
    connection management call elapsed        1660
    failed parse elapsed time                    0
    failed parse (out of shared memory)          0
    hard parse (sharing criteria) elaps          0
    hard parse (bind mismatch) elapsed           0
    PL/SQL execution elapsed time               95
    inbound PL/SQL rpc elapsed time              0
    PL/SQL compilation elapsed time              0
    Java execution elapsed time                  0
    repeated bind elapsed time                  48
    RMAN cpu time (backup/restore)               0Seems to be using a bit of time for the hard parse (hard parse elapsed time). Wait a little while, then re-execute the query:
    STAT_NAME                                VALUE
    DB time                                   9247
    DB CPU                                    9247
    background elapsed time                      0
    background cpu time                          0
    sequence load elapsed time                   0
    parse time elapsed                        6374
    hard parse elapsed time                   5997
    sql execute elapsed time                  2939
    connection management call elapsed        1660
    failed parse elapsed time                    0
    failed parse (out of shared memory)          0
    hard parse (sharing criteria) elaps          0
    hard parse (bind mismatch) elapsed           0
    PL/SQL execution elapsed time               95
    inbound PL/SQL rpc elapsed time              0
    PL/SQL compilation elapsed time              0
    Java execution elapsed time                  0
    repeated bind elapsed time                  48
    RMAN cpu time (backup/restore)               0The session is not reporting additional CPU usage or parse time.
    Let's check one of the session's statistics:
    SELECT
      SS.VALUE
    FROM
      V$SESSTAT SS,
      V$STATNAME SN
    WHERE
      SN.NAME='consistent gets'
      AND SN.STATISTIC#=SS.STATISTIC#
      AND SS.SID=310;
         VALUE
           163Not many consistent gets after 20+ minutes.
    Let's take a look at the plan:
    SQL> SELECT SQL_ID,CHILD_NUMBER FROM V$SQL WHERE SQL_TEXT LIKE 'select 1 from du
    al%';
    SQL_ID        CHILD_NUMBER
    04mpgrzhsv72w            0
    SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('04mpgrzhsv72w',0,'TYPICAL'))
    select 1 from dual where regexp_like   (' ','^*[ ]*a')
    NOTE: cannot fetch plan for SQL_ID: 04mpgrzhsv72w, 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_p
    lan)No plan...
    Let's take a look at the 10053 trace file:
    Registered qb: SEL$1 0x19157f38 (PARSER)
      signature (): qb_name=SEL$1 nbfros=1 flg=0
        fro(0): flg=4 objn=258 hint_alias="DUAL"@"SEL$1"
    Predicate Move-Around (PM)
    PM: Considering predicate move-around in SEL$1 (#0).
    PM:   Checking validity of predicate move-around in SEL$1 (#0).
    CBQT: Validity checks failed for 7uqx4guu04x3g.
    CVM: Considering view merge in query block SEL$1 (#0)
    CBQT: Validity checks failed for 7uqx4guu04x3g.
    Subquery Unnest
    SU: Considering subquery unnesting in query block SEL$1 (#0)
    Set-Join Conversion (SJC)
    SJC: Considering set-join conversion in SEL$1 (#0).
    Predicate Move-Around (PM)
    PM: Considering predicate move-around in SEL$1 (#0).
    PM:   Checking validity of predicate move-around in SEL$1 (#0).
    PM:     PM bypassed: Outer query contains no views.
    FPD: Considering simple filter push in SEL$1 (#0)
    FPD:   Current where clause predicates in SEL$1 (#0) :
              REGEXP_LIKE (' ','^*[ ]*a')
    kkogcp: try to generate transitive predicate from check constraints for SEL$1 (#0)
    predicates with check contraints:  REGEXP_LIKE (' ','^*[ ]*a')
    after transitive predicate generation:  REGEXP_LIKE (' ','^*[ ]*a')
    finally:  REGEXP_LIKE (' ','^*[ ]*a')
    apadrv-start: call(in-use=592, alloc=16344), compile(in-use=37448, alloc=42256)
    kkoqbc-start
                : call(in-use=592, alloc=16344), compile(in-use=38336, alloc=42256)
    kkoqbc-subheap (create addr=000000001915C238)Looks like the query never had a chance to start executing - it is still parsing after 20 minutes.
    I am not sure that this is a good example - the query either executes very fast, or never has a chance to start executing. But, it might still make your point physical I/O is not always the problem when performance problems are experienced.
    Charles Hooper
    IT Manager/Oracle DBA
    K&M Machine-Fabricating, Inc.

  • Forcing a query to use a differnet plan

    I have one database where a sql plan appears to be correct through tests. In another database the join order is flipped. Data is relatively close. Hinting the query would require a code change. What is the easiest to force the plan from the other database?
    if this new plan does not work, what is the easiest way to revert back.

    Hi,
    Another option is SQL Plan Management
    SQL Plan Management (SPM) prevents performance regressions resulting from sudden changes to the execution plan of a SQL statement by providing components for capturing, selecting and evolving SQL plan information. Changes to the execution plan may result from database upgrades, system and data changes, application upgrade or bug fixes. When SPM is enabled, the system maintains a plan history that contains all plans generated by the optimizer and stores them in a component called plan baseline. Among the plan history in the plan baseline, plans that are verified not to cause performance regression are marked as acceptable. The plan baseline is used by the optimizer to decide on the best plan to use when compiling a SQL statement. Repository stored in a data dictionary of plan baselines and statement log maintained by the optimizer is called SQL Management Base (SMB).
    http://www.oracle.com/technology/pub/articles/oracle-database-11g-top-features/11g-sqlplanmanagement.html
    Regards,

  • How to capture the execution plan for a query

    HI All,
    Can anyone please help me in finding out the command to capture the execution plan for a query.
    Execution plan for select * from EMP where <Condtions>
    it is getting executed successfully but i need to get the proper execution plan for the same.
    Thanks

    971830 wrote:
    i want to know where execution plan gets generated??
    in PMON of server process or in shared pool??
    i know that optimixer create execution plan..It is stored in Library Cache (present inside Shared Pool ).
    select * from v$sql_plan;An absolute beautiful white paper :
    Refer this -- www.sagelogix.com/sagelogix/SearchResults/SAGE015052
    Also -- http://www.toadworld.com/KNOWLEDGE/KnowledgeXpertforOracle/tabid/648/TopicID/XPVSP/Default.aspx
    HTH
    Ranit B.

  • How can I get the query name/aggregationslevel in my planning function?

    Hi,
    is it possiable to get the query name/aggregationslevel in a planning fuction?
    thanks for your idea.
    hongwei

    Hi,
    The interface IF_RSPLFA_INFOPROV_DESC
    provides methods for retrieving the most important properties of an InfoProvider from the point of view of the implementation class of a planning function type.
    Methods GET_T_CHARNM and GET_T_KEYFNM return the names of the characteristics and key figures in the InfoProvider.
    You can get the properties of the InfoObjects and compound information by calling method GET_TAB_IOBJ_PRO and GET_TAB_IOBJ_CMP .
    Does this help?
    Best Regards
    Shyam

  • Different query plans for same query on same DB

    Hi,
    HP-Ux
    Oracle Database 10.2.0.4
    We are experiencing a strange issue. One of our night batch process is taking invariably more time to execute. The process does not consume time at 1 particular query. Everyday we find a new query taking more time than previous execution.
    Now, when we see the explain plan while the query is executing, we see NESTED LOOP SEMI (with improper index being used). At the same time if we take the query and see the explain plan seperately, we get HASH JOIN SEMI (with proper index being used). Also, if we execute this query with the values as in procedure, it finishes within mili seconds (as it should).
    The tables and indexes are analyzed everyday before the process starts.
    Can anybody explain, why the same query shows two different plans at the same time ?
    Thanks a lot in advance :)

    Aalap Sharma wrote:
    HP-Ux
    Oracle Database 10.2.0.4
    We are experiencing a strange issue. One of our night batch process is taking invariably more time to execute. The process does not consume time at 1 particular query. Everyday we find a new query taking more time than previous execution.
    Now, when we see the explain plan while the query is executing, we see NESTED LOOP SEMI (with improper index being used). At the same time if we take the query and see the explain plan seperately, we get HASH JOIN SEMI (with proper index being used). Also, if we execute this query with the values as in procedure, it finishes within mili seconds (as it should).
    The tables and indexes are analyzed everyday before the process starts.
    Can anybody explain, why the same query shows two different plans at the same time ?As already mentioned, you might hit typical issues in 10.2: The column workload based SIZE AUTO statistics gathering feature and/or bind variable peeking.
    How do you analyze the tables and indexes before the process starts? Can you share the exact call with parameters?
    Some ideas:
    1. If your process is "new", then the column workload monitoring of the database might recognize the column usage pattern and generate histograms on some of your columns. It might take a while until the workload has been established so that all columns got histograms according to the workload (It needs a certain number of usages/executions before the workload is registered as relevant). Until then you might get different execution plans each time the statistics are refreshed due to new histograms being added.
    2. If the default 10g statistics gathering job is active, it might gather different statistics during the night than your individual job that runs prior to the processing. This could be one possible explanation why you get different plans on the next day.
    3. "Bind Variable Peeking" is possibly another issue you might run into. How do you test the query so that you get a different, well performing plan? Does your original statement use bind variables? Do you use literals to reproduce? Note that using EXPLAIN PLAN on statements involving bind variables can lie, since it doesn't perform bind variable peeking by default.
    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/

  • How to skip existing execution plan for a query

    Hi,
    I want to skip existng execution plan for a query which I am executing often. I dont want it to use the same execution plan everytime. Please let me know if any method is there skip the existing execution plan.
    Thanks in advance.......
    Edited by: 900105 on Dec 1, 2011 4:52 AM

    Change the query so it is syntactically different, but has the same semantics (meaning). That way CBO will reparse it and you might get a new execution plan.
    One simple way to do that is to add a dummy predicate ( 45=45) to the where clause. The predicate must be changed every time the query is executed ( 46=46 , 47=47 ,… ).
    Iordan Iotzov
    http://iiotzov.wordpress.com/

  • Explain Plan of a query.

    I want to know the concept of this 'Explain Plan' of a query & why it is used. Could u also give the syntax of how a explain plan for a query can be done. Please help in solving the doubt as it is urgent.

    Hi,
    set autotrace traceonly explain or
    -- create your plan table in your schema (only once)
    @ ?/rdbms/admin/utlxplan
    -- populate the plan table for your select
    explain plan for select ...;
    -- display output out of the PLAN_TABLE
    @ ?/rdbms/admin/ultxplsRegards
    Laurent

  • Differenet Explain Plan for Same Query

    DB Version : 11.2.0.3
    OS Version : AIX 6
    I have two Queries ( The Difference between Them Only 940 and 584 ) When I Generate Explain Plan Different Output Why ? Why CPU time is Different Each Time
    First Query Statement  :
    INSERT INTO TempSearchResult (t_aid,
                                  t_umidl,
                                  t_umidh,
                                  X_CREA_DATE_TIME_MESG)
       SELECT z.aid,
              z.mesg_s_umidl,
              z.mesg_s_umidh,
              z.mesg_crea_date_time
         FROM (  SELECT m.aid,
                        m.mesg_s_umidl,
                        m.mesg_s_umidh,
                        m.mesg_crea_date_time
                   FROM RSMESG_ESIDE m
                  WHERE 1 = 1
                        AND m.mesg_crea_date_time BETWEEN TO_DATE (
                                                             '20120131 10:00:00',
                                                             'YYYYMMDD HH24:MI:SS')
                                                      AND TO_DATE (
                                                             '20120131 13:00:00',
                                                             'YYYYMMDD HH24:MI:SS')
                        AND m.mesg_frmt_name = 'Swift'
                        AND m.mesg_sender_x1 = 'SOGEFRPPXXX'
                        AND m.mesg_nature = 'FINANCIAL_MSG'
                        AND m.mesg_type LIKE '950'
               ORDER BY mesg_crea_date_time) z
        WHERE ROWNUM <= 5000
    Explain Plan for First Query :
    PLAN_TABLE_OUTPUT
    Plan hash value: 3901722890
    | Id  | Operation                                 | Name              | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
    |   0 | INSERT STATEMENT                          |                   |  2866 |   134K|   197   (3)| 00:00:03 |       |       |
    |   1 |  LOAD TABLE CONVENTIONAL                  | TEMPSEARCHRESULT  |       |       |            |          |       |       |
    |*  2 |   COUNT STOPKEY                           |                   |       |       |            |          |       |       |
    |   3 |    VIEW                                   |                   |  2866 |   134K|   197   (3)| 00:00:03 |       |       |
    |*  4 |     SORT ORDER BY STOPKEY                 |                   |  2866 |   333K|   197   (3)| 00:00:03 |       |       |
    |   5 |      NESTED LOOPS                         |                   |  2866 |   333K|   196   (2)| 00:00:03 |       |       |
    PLAN_TABLE_OUTPUT
    |   6 |       NESTED LOOPS                        |                   |  1419 |   148K|   196   (2)| 00:00:03 |       |       |
    |*  7 |        HASH JOIN                          |                   |  1419 |   141K|   196   (2)| 00:00:03 |       |       |
    |   8 |         NESTED LOOPS                      |                   |    91 |  1911 |     2   (0)| 00:00:01 |       |       |
    |   9 |          TABLE ACCESS BY INDEX ROWID      | SUSER             |     1 |    10 |     1   (0)| 00:00:01 |       |       |
    |* 10 |           INDEX UNIQUE SCAN               | IX_SUSER          |     1 |       |     0   (0)| 00:00:01 |       |       |
    |* 11 |          INDEX FULL SCAN                  | PK_SUNITUSERGROUP |    91 |  1001 |     1   (0)| 00:00:01 |       |       |
    |  12 |         PARTITION RANGE SINGLE            |                   |  1450 |   114K|   193   (2)| 00:00:03 |     2 |     2 |
    |* 13 |          TABLE ACCESS BY LOCAL INDEX ROWID| RMESG             |  1450 |   114K|   193   (2)| 00:00:03 |     2 |     2 |
    |* 14 |           INDEX SKIP SCAN                 | IX_RMESG          |   415 |       |    14  (15)| 00:00:01 |     2 |     2 |
    |* 15 |        INDEX UNIQUE SCAN                  | PK_SMSGUSERGROUP  |     1 |     5 |     0   (0)| 00:00:01 |       |       |
    |* 16 |       INDEX UNIQUE SCAN                   | PK_SBICUSERGROUP  |     2 |    24 |     0   (0)| 00:00:01 |       |       |
    PLAN_TABLE_OUTPUT
    PLAN_TABLE_OUTPUT
    Predicate Information (identified by operation id):
       2 - filter(ROWNUM<=5000)
       4 - filter(ROWNUM<=5000)
       7 - access("X_INST0_UNIT_NAME"="UNIT")
      10 - access("SUSER"."USERNAME"="SIDE"."GETMYUSER"())
      11 - access("SUSER"."GROUPID"="SUNITUSERGROUP"."GROUPID")
           filter("SUSER"."GROUPID"="SUNITUSERGROUP"."GROUPID")
    PLAN_TABLE_OUTPUT
      13 - filter("RMESG"."MESG_SENDER_X1"='SOGEFRPPXXX' AND "RMESG"."MESG_NATURE"='FINANCIAL_MSG' AND
                  "RMESG"."MESG_FRMT_NAME"='Swift')
      14 - access("RMESG"."MESG_CREA_DATE_TIME">=TO_DATE(' 2012-01-31 10:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
                  "RMESG"."MESG_TYPE"='950' AND "RMESG"."MESG_CREA_DATE_TIME"<=TO_DATE(' 2012-01-31 13:00:00', 'syyyy-mm-dd hh24:mi:ss'))
           filter("RMESG"."MESG_TYPE"='950')
      15 - access("X_CATEGORY"="CATEGORY" AND "SUSER"."GROUPID"="SMSGUSERGROUP"."GROUPID")
      16 - access("X_OWN_LT"="BICCODE" AND "SUSER"."GROUPID"="SBICUSERGROUP"."GROUPID")
    40 rows selected.
    Second query
    INSERT INTO TempSearchResult (t_aid,
                                  t_umidl,
                                  t_umidh,
                                  X_CREA_DATE_TIME_MESG)
       SELECT z.aid,
              z.mesg_s_umidl,
              z.mesg_s_umidh,
              z.mesg_crea_date_time
         FROM (  SELECT  m.aid,
                        m.mesg_s_umidl,
                        m.mesg_s_umidh,
                        m.mesg_crea_date_time
                   FROM RSMESG_ESIDE m
                  WHERE 1 = 1
                        AND m.mesg_crea_date_time BETWEEN TO_DATE (
                                                             '20120117 10:00:00',
                                                             'YYYYMMDD HH24:MI:SS')
                                                      AND TO_DATE (
                                                             '20120117 13:00:00',
                                                             'YYYYMMDD HH24:MI:SS')
                        AND m.mesg_frmt_name = 'Swift'
                        AND m.mesg_sender_x1 = 'SOGEFRPPGSS'
                        AND m.mesg_nature = 'FINANCIAL_MSG'
                        AND m.mesg_type LIKE '548'
               ORDER BY mesg_crea_date_time) z
        WHERE ROWNUM <= 5000
    Explain Plan For Second Query :
    PLAN_TABLE_OUTPUT
    Plan hash value: 4106071428
    | Id  | Operation                                  | Name              | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     | Pstart| Pstop |
    |   0 | INSERT STATEMENT                           |                   |  1073 | 51504 |       |  2622   (1)| 00:00:32 |       |       |
    |   1 |  LOAD TABLE CONVENTIONAL                   | TEMPSEARCHRESULT  |       |       |       |            |          |       |       |
    |*  2 |   COUNT STOPKEY                            |                   |       |       |       |            |          |       |       |
    |   3 |    VIEW                                    |                   |  1073 | 51504 |       |  2622   (1)| 00:00:32 |       |       |
    |*  4 |     SORT ORDER BY STOPKEY                  |                   |  1073 |   124K|       |  2622   (1)| 00:00:32 |       |       |
    |   5 |      NESTED LOOPS                          |                   |  1073 |   124K|       |  2621   (1)| 00:00:32 |       |       |
    PLAN_TABLE_OUTPUT
    |   6 |       NESTED LOOPS                         |                   |   531 | 56817 |       |  2621   (1)| 00:00:32 |       |       |
    |   7 |        NESTED LOOPS                        |                   |   531 | 54162 |       |  2621   (1)| 00:00:32 |       |       |
    |   8 |         NESTED LOOPS                       |                   |   543 | 49413 |       |  2621   (1)| 00:00:32 |       |       |
    |   9 |          TABLE ACCESS BY INDEX ROWID       | SUSER             |     1 |    10 |       |     1   (0)| 00:00:01 |       |       |
    |* 10 |           INDEX UNIQUE SCAN                | IX_SUSER          |     1 |       |       |     0   (0)| 00:00:01 |       |       |
    |  11 |          PARTITION RANGE SINGLE            |                   |   543 | 43983 |       |  2621   (1)| 00:00:32 |     2 |     2 |
    |* 12 |           TABLE ACCESS BY LOCAL INDEX ROWID| RMESG             |   543 | 43983 |       |  2621   (1)| 00:00:32 |     2 |     2 |
    |  13 |            BITMAP CONVERSION TO ROWIDS     |                   |       |       |       |            |          |       |       |
    |  14 |             BITMAP AND                     |                   |       |       |       |            |          |       |       |
    |  15 |              BITMAP CONVERSION FROM ROWIDS |                   |       |       |       |            |          |       |       |
    |* 16 |               INDEX RANGE SCAN             | IX_SENDER         | 25070 |       |       |   894   (1)| 00:00:11 |     2 |     2 |
    PLAN_TABLE_OUTPUT
    |  17 |              BITMAP CONVERSION FROM ROWIDS |                   |       |       |       |            |          |       |       |
    |  18 |               SORT ORDER BY                |                   |       |       |   408K|            |          |       |       |
    |* 19 |                INDEX RANGE SCAN            | IX_RMESG          | 25070 |       |       |  1405   (1)| 00:00:17 |     2 |     2 |
    |* 20 |         INDEX UNIQUE SCAN                  | PK_SUNITUSERGROUP |     1 |    11 |       |     0   (0)| 00:00:01 |       |       |
    |* 21 |        INDEX UNIQUE SCAN                   | PK_SMSGUSERGROUP  |     1 |     5 |       |     0   (0)| 00:00:01 |       |       |
    |* 22 |       INDEX UNIQUE SCAN                    | PK_SBICUSERGROUP  |     2 |    24 |       |     0   (0)| 00:00:01 |       |       |
    Predicate Information (identified by operation id):
    PLAN_TABLE_OUTPUT
       2 - filter(ROWNUM<=5000)
       4 - filter(ROWNUM<=5000)
      10 - access("SUSER"."USERNAME"="SIDE"."GETMYUSER"())
      12 - filter("RMESG"."MESG_NATURE"='FINANCIAL_MSG' AND "RMESG"."MESG_FRMT_NAME"='Swift')
      16 - access("RMESG"."MESG_SENDER_X1"='SOGEFRPPGSS')
      19 - access("RMESG"."MESG_CREA_DATE_TIME">=TO_DATE(' 2012-01-17 10:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
                  "RMESG"."MESG_TYPE"='548' AND "RMESG"."MESG_CREA_DATE_TIME"<=TO_DATE(' 2012-01-17 13:00:00', 'syyyy-mm-dd hh24:mi:ss'))
           filter("RMESG"."MESG_TYPE"='548' AND "RMESG"."MESG_CREA_DATE_TIME"<=TO_DATE(' 2012-01-17 13:00:00', 'syyyy-mm-dd
                  hh24:mi:ss') AND "RMESG"."MESG_CREA_DATE_TIME">=TO_DATE(' 2012-01-17 10:00:00', 'syyyy-mm-dd hh24:mi:ss'))
      20 - access("X_INST0_UNIT_NAME"="UNIT" AND "SUSER"."GROUPID"="SUNITUSERGROUP"."GROUPID")
      21 - access("X_CATEGORY"="CATEGORY" AND "SUSER"."GROUPID"="SMSGUSERGROUP"."GROUPID")
    PLAN_TABLE_OUTPUT
      22 - access("X_OWN_LT"="BICCODE" AND "SUSER"."GROUPID"="SBICUSERGROUP"."GROUPID")
    45 rows selected.
    Table Structure TEMPSEARCHRESULT
    CREATE GLOBAL TEMPORARY TABLE TEMPSEARCHRESULT
      T_AID                  NUMBER(3),
      T_UMIDL                NUMBER(10),
      T_UMIDH                NUMBER(10),
      X_CREA_DATE_TIME_MESG  DATE
    ON COMMIT PRESERVE ROWS
    NOCACHE;
    CREATE INDEX SIDE.TEMP_SEARCH_INDEX ON SIDE.TEMPSEARCHRESULT
    (T_AID, T_UMIDL, T_UMIDH, X_CREA_DATE_TIME_MESG);

    Again Thank you For your amazing Answer.
    For indexes it's a balance. Check this query which is Simple
    Select * from RMESGI generated Explain Plan for it to see effect of indexes .
    PLAN_TABLE_OUTPUT
    Plan hash value: 1686435785
    | Id  | Operation           | Name  | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
    |   0 | SELECT STATEMENT    |       |    11M|  8920M|   376K  (1)| 01:15:20 |      |        |
    |   1 |  PARTITION RANGE ALL|       |    11M|  8920M|   376K  (1)| 01:15:20 |    1 |     12 |
    |   2 |   TABLE ACCESS FULL | RMESG |    11M|  8920M|   376K  (1)| 01:15:20 |    1 |     12 |
    ---------------------------------------------------------------------------------------------1:15:20 For table access and Full Scan Also , I generate new Indexes on the table like the following
    CREATE TABLE RMESG(
            aid NUMBER(3) NOT NULL,
            mesg_s_umidl NUMBER(10) NOT NULL,
            mesg_s_umidh NUMBER(10) NOT NULL,
            mesg_validation_requested CHAR(18) NOT NULL,
            mesg_validation_passed CHAR(18) NOT NULL,
            mesg_class CHAR(16) NOT NULL,
            mesg_is_text_readonly NUMBER(1) NOT NULL,
            mesg_is_delete_inhibited NUMBER(1) NOT NULL,
            mesg_is_text_modified NUMBER(1) NOT NULL,
            mesg_is_partial NUMBER(1) NOT NULL,
            mesg_crea_mpfn_name CHAR(24) NOT NULL,
            mesg_crea_rp_name CHAR(24) NOT NULL,
            mesg_crea_oper_nickname CHAR(151) NOT NULL,
            mesg_crea_date_time DATE NOT NULL,
            mesg_mod_oper_nickname CHAR(151) NOT NULL,
            mesg_mod_date_time DATE NOT NULL,
            mesg_frmt_name VARCHAR2(17) NOT NULL,
            mesg_nature CHAR(14) NOT NULL,
            mesg_sender_x1 CHAR(11) NOT NULL,
            mesg_sender_corr_type VARCHAR2(24) NOT NULL,
            mesg_uumid VARCHAR2(50) NOT NULL,
            mesg_uumid_suffix NUMBER(10) NOT NULL,
            x_own_lt CHAR(8) NOT NULL,
            x_inst0_unit_name VARCHAR2(32) default 'NONE' NOT NULL,
            x_category CHAR(1) NOT NULL,
            archived NUMBER(1) NOT NULL,
            restored NUMBER(1) NOT NULL,
            mesg_related_s_umid CHAR(16) NULL,
            mesg_status CHAR(12) NULL,
            mesg_crea_appl_serv_name CHAR(24) NULL,
            mesg_verf_oper_nickname CHAR(151) NULL,
            mesg_data_last NUMBER(10) NULL,
            mesg_token NUMBER(10) NULL,
            mesg_batch_reference VARCHAR2(46) NULL,
            mesg_cas_sender_reference VARCHAR2(40) NULL,
            mesg_cas_target_rp_name VARCHAR2(20) NULL,
            mesg_ccy_amount VARCHAR2(501) NULL,
            mesg_copy_service_id VARCHAR2(4) NULL,
            mesg_data_keyword1 VARCHAR2(80) NULL,
            mesg_data_keyword2 VARCHAR2(80) NULL,
            mesg_data_keyword3 VARCHAR2(80) NULL,
            mesg_delv_overdue_warn_req NUMBER(1) NULL,
            mesg_fin_ccy_amount VARCHAR2(24) NULL,
            mesg_fin_value_date CHAR(6) NULL,
            mesg_is_live NUMBER(1) NULL,
            mesg_is_retrieved NUMBER(1) NULL,
            mesg_mesg_user_group VARCHAR2(24) NULL,
            mesg_network_appl_ind CHAR(3) NULL,
            mesg_network_delv_notif_req NUMBER(1) NULL,
            mesg_network_obso_period NUMBER(10) NULL,
            mesg_network_priority CHAR(12) NULL,
            mesg_possible_dup_creation VARCHAR2(8) NULL,
            mesg_receiver_alia_name VARCHAR2(32) NULL,
            mesg_receiver_swift_address CHAR(12) NULL,
            mesg_recovery_accept_info VARCHAR2(80) NULL,
            mesg_rel_trn_ref VARCHAR2(80) NULL,
            mesg_release_info VARCHAR2(32) NULL,
            mesg_security_iapp_name VARCHAR2(80) NULL,
            mesg_security_required NUMBER(1) NULL,
            mesg_sender_x2 VARCHAR2(21) NULL,
            mesg_sender_x3 VARCHAR2(21) NULL,
            mesg_sender_x4 VARCHAR2(21) NULL,
            mesg_sender_branch_info VARCHAR2(71) NULL,
            mesg_sender_city_name VARCHAR2(36) NULL,
            mesg_sender_ctry_code VARCHAR2(3) NULL,
            mesg_sender_ctry_name VARCHAR2(71) NULL,
            mesg_sender_institution_name VARCHAR2(106) NULL,
            mesg_sender_location VARCHAR2(106) NULL,
            mesg_sender_swift_address CHAR(12) NULL,
            mesg_sub_format VARCHAR2(6) NULL,
            mesg_syntax_table_ver VARCHAR2(8) NULL,
            mesg_template_name VARCHAR2(32) NULL,
            mesg_trn_ref VARCHAR2(16) NULL,
            mesg_type CHAR(3) NULL,
            mesg_user_issued_as_pde NUMBER(1) NULL,
            mesg_user_priority_code CHAR(4) NULL,
            mesg_user_reference_text VARCHAR2(30) NULL,
            mesg_zz41_is_possible_dup NUMBER(1) NULL,
            x_fin_ccy CHAR(3) NULL,
            x_fin_amount NUMBER(21,4) NULL,
            x_fin_value_date DATE NULL,
            x_fin_ocmt_ccy CHAR(3) NULL,
            x_fin_ocmt_amount NUMBER(21,4) NULL,
            x_receiver_x1 CHAR(11) NULL,
            x_receiver_x2 VARCHAR2(21) NULL,
            x_receiver_x3 VARCHAR2(21) NULL,
            x_receiver_x4 VARCHAR2(21) NULL,
            last_update DATE NULL,
            set_id NUMBER(10) NULL,
            mesg_requestor_dn VARCHAR2(101) NULL,
            mesg_service VARCHAR2(31) NULL,
            mesg_request_type VARCHAR2(31) NULL,
            mesg_identifier VARCHAR2(31) NULL,
            mesg_xml_query_ref1 VARCHAR2(101) NULL,
            mesg_xml_query_ref2 VARCHAR2(101) NULL,
            mesg_xml_query_ref3 VARCHAR2(101) NULL,
            mesg_appl_sender_reference VARCHAR2(51) NULL,
            mesg_payload_type VARCHAR2(31) NULL,
            mesg_sign_digest_reference VARCHAR2(41) NULL,
            mesg_sign_digest_value VARCHAR2(51) NULL,
            mesg_use_pki_signature NUMBER(1) NULL
    PARTITION BY RANGE(MESG_CREA_DATE_TIME) (
        PARTITION SIDE_MIN VALUES LESS THAN (TO_DATE(20000101, 'YYYYMMDD')) TABLESPACE TBS_SIDEDB_DA_01);
    CREATE UNIQUE INDEX SIDE.IX_PK_RMESG on SIDE.RMESG (AID, MESG_S_UMIDH, MESG_S_UMIDL, MESG_CREA_DATE_TIME) LOCAL;
    ALTER TABLE SIDE.RMESG ADD CONSTRAINT IX_PK_RMESG PRIMARY KEY (AID, MESG_S_UMIDH, MESG_S_UMIDL, MESG_CREA_DATE_TIME) USING INDEX SIDE.IX_PK_RMESG;
    CREATE INDEX SIDE.ix_rmesg_cassender ON SIDE.rmesg (MESG_CAS_SENDER_REFERENCE) LOCAL;
    CREATE INDEX SIDE.ix_rmesg_creationdate ON SIDE.rmesg (MESG_CREA_DATE_TIME) LOCAL;
    CREATE INDEX SIDE.ix_rmesg_trnref ON SIDE.rmesg (MESG_TRN_REF) LOCAL;
    CREATE INDEX SIDE.ix_rmesg_uumid ON SIDE.rmesg (MESG_UUMID, MESG_UUMID_SUFFIX) LOCAL;
    CREATE INDEX SIDE.IX_UNIT_NAME_RMESG on RMESG(mesg_crea_date_time,X_INST0_UNIT_NAME) LOCAL;
    CREATE INDEX SIDE.IX_RMESG on RMESG(mesg_crea_date_time ,mesg_type,x_fin_ccy) LOCAL;
    CREATE INDEX SIDE.IX_NAME_FORMAT_TYPE_RMESG on RMESG(mesg_frmt_name,mesg_sub_format,mesg_type,mesg_crea_date_time ) LOCAL;same Explain Plan Same Result .
    I always remember TOM Quote "full scans are not evil, indexes are not good"
    Which Mean Something Wrong Goes with Indexes , the partition depend on MESG_CREA_DATE_TIME Column I create Index for this column but same explain plan Appear every time. With Same Time.
    Thank you
    Osama

  • Is there a way to stop a query just after the cursor/plan is produced by CBO?

    Following suggestions of Kerry Osborne&amp;#8217;s Oracle Blog &amp;raquo; Blog Archive &amp;raquo; Explain Plan Lies &amp;#8211; Kerry Osborn…
    on the lies of "Explain plan" (and of "set autotrace on"  too) I'd like to try to stop a query/DML before actually it starts, just after the plan was produced and sql_id assigned.
    Is there any CLEAN way (other than trying CTRL-C) to do that?
    Thanks
    Paolo

    Hi
    PaolFili wrote:
    Thanks rp.
    I think my question is a little dofferent, but your reply give me an idea.(which has clear disadvantages , but can do the work).
    The problem is obtain in Lybrary Cache a plan,a SLQ_ID,PLAN_HASH for a query ( i.e. a 10days running query) that I cannot start-up.
    So my (suggested from your reply) idea is:
    1) to LOCK EXCLUSIVE (a table level) , **if it's possible** every table accessed in the query ( Yes, it can be really expansive in some production environment, but sometimes can be necessary ..)
      using :  LOCK TABLE table IN EXCLUSIVE MODE
    for each table accessed from query
    2) Startup the query that will be suspended form the lock on tables accessed + kill the sid,serial#,@Inst_id for the query.
    3) UNLOCK tables from EXCLUSIVE using "ROLLBACK"  in the session where LOCK TABLE.... was send.(to remake  tables to work for other queries)
    Any other ideas?
    Thanks
    Paolo
    you're planning on using locks to stop a query and you think in order to do so you need exclusive locks  on every table accessed in the query? And you are prepared to do that on a production system?
    And all of this is needed to troubleshoot a query that was running for 10 days -- i.e. a query that was available for all kinds of diagnostics during 10 days?
    Sorry, I think it wasn't a good idea for you to read that Osborne's blog post -- you should've started with more basic things. Way more basic.
    Best regards,
      Nikolay

Maybe you are looking for

  • Trying to scan from a HP1315

    I am trying to scan an image from a HP1315 printer while running OSX-10.8.6. Can anyone tell me if there is a standard app that will do?

  • Redhat linux 6 VM - stop and  restart

    Hi, I've installed new HVM - Redhat Linux 6, standard installation. I have the problem with stop and restart initiated from the OVM console. Job is running, but the OS inside VM doesn't start the shutdown process. For example another VM running Oracl

  • Installing Photoshop 6 on Windows 7

    I have Photoshop 6, upgrade 7 and upgrade CS2. They were all on my old laptop with Windows XP system. I have a newer computer I wish to install CS2 on, which has Windows 7. The CS2 will install but needs the earlier versions of Photoshop to be able t

  • Broadcasting: To automate the process of E-mailing reports to users

    Hi, Can you share in details the approach to automatically e-mail query outputs at specific times to specific users? Is the right term “broadcast” in this case? Thanks

  • Customizing Documentation from Solution Manager

    Morning, We made project customizing via Solution Manager, and our customer would like to have a report or customizing documentation from SM. How to generate this document via Solution Manager Thank You in advance DP