Performance tests: bind variables

When you write a SQL for the application, I want to do some performance testing of SQL before providing to JAVA developers. I understand that I need to test using bind variables, can someone guide me how do? What are the best tools out there to achieve? Please provide some guidance. Thank you!

Rinne says:
I read more about bind variable and I can test the use of bind variables. I understand that testing SQL using bind variables is a closer representation of the real world. Even when queries have large tables, the performance would be similar if I run the query using literals twice (to avoid hard analysis) compared to tests with bind variables? I am trying to understand more thoroughly the need for tests with bind variables. Why we take more time than the other? Thank you!

The main thing is that the opimizer will / can do if bind variables different optimization techniques are used or not.
There are two contradictory effects its regarding the binding settings.

Effect 1: Reuse of cursor
If the same is done again and again and again. Maybe different sessions. Then using binding settings is essential. Why? Because the same cursor can be reused, which saves a lot of time for analysis. This occurs mainly in OLTP systems. The idea behind this is that to do the same action, just for a different ID (ID in order for example) will result in an identical implementation plan.

The result: Faster analysis time and less consumption of memory, because the same cursor can be resused between different sessions.

Effect 2: Correct assumptions

According to a filter expression, some using the value of the OBC will make an assumption based on statistical data the number of rows is returned because of this expression and the value.
Speculation between a literal value and a bound value may be different. In many cases exist technical opimization (bind peeking etc.) to the same literal conjecture binded proposal.

But there are exceptions, for example a condition such as the following will result in different estimates

column between 10 and 20
column between :P1 and :P2

There are as well other effects.

Result: The CBO can make assumptions better if literals. But most of the cases the proposal is identical.

Conclusion: Literal values are useful if you do large queries where the output size depends strongly on the parameters provided, and where you run that very few of these (OLAP) queries.
Binding settings are usfull when the same execution (OLTP) need a grand plan number of queries.

Tags: Database

Similar Questions

  • Bind variables improve query performance?

    Hello

    Bind variables improve the performance of the program or a SQL query.

    Select empno, ename, sal from emp where empno =: eno;

    Select sal in: emp WNV;

    According to these queries I request performance of the bind variable to learn more from you.



    Kind regards
    Vincent.

    Published by: Venkata2 on September 16, 2008 18:40

    any variable inside a stored procedure is one of liaison.

    the use of bind variables can help you avoid analysis difficult. which leads to the improvement of the performance.

    Thank you
    Knani.

  • Histograms and bind variables

    Hi guys,.
    When a SQL using bind variables histograms afftect the excution plan how?

    For example
    There is a table 'TEST' a column a number, varchar2 (100) b, c. tank (100).
    100000 rows in this table and the 90000 column rows a value is 1, the other value is 2-10 by 100rows.

    Now this column a histogram and a btree index only has on a;

    Here is sample code
    number of a_v var;
    exec: a_v: = 10;

    Select * from test where a =: a_v.

    The plan of the excution is full table scan.

    If I don't use of variable binding. It will scan the index.


    I remember that a document mentioned that "do not use histograms using bind variables.

    But why?

    I disabled same bind variable peeking?


    Thank you guys. Looking forward to your response.

    Longfei Wei says:
    Thanks Centinul this article is useful for me, but it is not explained why histograms do not work well with bind variables.

    Thanks again.

    Longfei,

    Jonathan Lewis article is very good - another look at this article. Histograms peuvent work with bind variable, but the end result is usually not the desired result. Bind variables are used to reduce the number of different execution plans. The histograms are used to find what is supposed to be the best execution plan for the predicates provided and in the case of the bind variables, those are peeked from the bind variable values. Thus, if you have a histogram on a column and for hard analysis of a SQL statement and the most common value in this column is presented in the binding variable - this execution plan is considered by the optimizer to be the 'best' execution plan for the bind variable values provided. Suppose now that instead the less popular value in the column is specified - the optimizer peut produce a very different for the same SQL statement execution plan, which is optimized for the less popular value (this can be a scan of the index systematic range, rather than a full table scan). Now suppose the execution plan may not change when change of variable values bind - if you have a single popular value and many unpopular values, if the analysis lasts is performed with the popular single value, you might find that all subsequent runs of this SQL statement to perform full table scans, even if only a few rows in the table are selected.

    Here's a quick test on the Oracle 11.2.0.2 database to demonstrate:

    CREATE TABLE T1 (
      C1 NUMBER,
      C2 NUMBER,
      C3 VARCHAR2(300));
    
    INSERT INTO
      T1
    SELECT
      *
    FROM
      (SELECT
        ROWNUM C1,
        DECODE(MOD(ROWNUM,100),99,99,1) C2,
        RPAD('A',300,'A') C3
      FROM
        DUAL
      CONNECT BY
        LEVEL <= 1000000)
    ORDER BY
      C2;
    
    CREATE INDEX IND_T1_C2 ON T1(C2);
    
    EXEC DBMS_STATS.GATHER_TABLE_STATS(OWNNAME=>USER,TABNAME=>'T1',CASCADE=>TRUE,METHOD_OPT=>'FOR ALL INDEXED COLUMNS SIZE 254')
    

    The foregoing has created a table with 1 000 000 lines where 99% of the lines have a value of 1 in C2 and 1% have a value of 99, and lines are inserted with a perfect setting in cluster factor because of the ORDER BY clause. A histogram has been created on the indexed column.

    Let's try a test, we'll search a unpopular value 2 for the connection variable:

    VARIABLE N1 NUMBER
    EXEC :N1:=2
    
    SELECT /*+ GATHER_PLAN_STATISTICS */
      C1,
      C2
    FROM
      T1
    WHERE
      C2 = :N1;
    
    no rows selected
    
    SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'ALLSTATS LAST'));
    
    SQL_ID  c7su63uw7nch6, child number 0
    -------------------------------------
    SELECT /*+ GATHER_PLAN_STATISTICS */   C1,   C2 FROM   T1 WHERE   C2 =
    :N1
    
    Plan hash value: 236868917
    
    ------------------------------------------------------------------------------------------------------------
    | Id  | Operation                   | Name      | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |
    ------------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT            |           |      1 |        |      0 |00:00:00.01 |       3 |      1 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| T1        |      1 |   5957 |      0 |00:00:00.01 |       3 |      1 |
    |*  2 |   INDEX RANGE SCAN          | IND_T1_C2 |      1 |   5957 |      0 |00:00:00.01 |       3 |      1 |
    ------------------------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       2 - access("C2"=:N1)
    

    Thus, there is no selected row, the optimizer predicts that 5 957 lines would be returned and a path to the index has been selected. Path of this index would also be appropriate for the value of the bind variable 1? We will continue the trial, this time by choosing the 99 for the binding variable value:

    EXEC :N1:=99
    SET TIMING ON
    
    SELECT /*+ GATHER_PLAN_STATISTICS */
      C1,
      C2
    FROM
      T1
    WHERE
      C2 = :N1;
    
    ...
    10000 rows selected.
    
    Elapsed: 00:00:05.35
    
    SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'ALLSTATS LAST'));
    
    SQL_ID  c7su63uw7nch6, child number 0
    -------------------------------------
    SELECT /*+ GATHER_PLAN_STATISTICS */   C1,   C2 FROM   T1 WHERE   C2 =
    :N1
    
    Plan hash value: 236868917
    
    ---------------------------------------------------------------------------------------------------
    | Id  | Operation                   | Name      | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
    ---------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT            |           |      1 |        |  10000 |00:00:00.02 |    1783 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| T1        |      1 |   5957 |  10000 |00:00:00.02 |    1783 |
    |*  2 |   INDEX RANGE SCAN          | IND_T1_C2 |      1 |   5957 |  10000 |00:00:00.01 |     690 |
    ---------------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       2 - access("C2"=:N1)
    

    Once more, the optimizer predicts 5 957 lines could be found even if 10,000 rows have been retrieved. Note also that the number of children is always 0. We will continue the trial, this time with the bind variable value of 1:

    EXEC :N1:=1
    
    SET AUTOTRACE TRACEONLY STATISTICS
    
    SELECT /*+ GATHER_PLAN_STATISTICS */
      C1,
      C2
    FROM
      T1
    WHERE
      C2 = :N1;
    
    990000 rows selected.
    
    Elapsed: 00:00:18.78
    
    Statistics
    ---------------------------------------------------
              1  recursive calls
              1  db block gets
         108571  consistent gets
              0  physical reads
             96  redo size
       21958348  bytes sent via SQL*Net to client
         726508  bytes received via SQL*Net from client
          66001  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
         990000  rows processed
    
    SET AUTOTRACE OFF
    

    Because I used AUTOTRACE to prevent 990 000 lines scrolling on the screen, I have to specify the SQL_ID and CHILD_NUMBER to retrieve the execution plan:

    
    SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('c7su63uw7nch6',0,'ALLSTATS LAST'));
    
    SQL_ID  c7su63uw7nch6, child number 0
    -------------------------------------
    SELECT /*+ GATHER_PLAN_STATISTICS */   C1,   C2 FROM   T1 WHERE   C2 =
    :N1
    
    Plan hash value: 236868917
    
    ---------------------------------------------------------------------------------------------------
    | Id  | Operation                   | Name      | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
    ---------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT            |           |      1 |        |  10000 |00:00:00.02 |    1783 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| T1        |      1 |   5957 |  10000 |00:00:00.02 |    1783 |
    |*  2 |   INDEX RANGE SCAN          | IND_T1_C2 |      1 |   5957 |  10000 |00:00:00.01 |     690 |
    ---------------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       2 - access("C2"=:N1)
    

    Who can be the execution plan that was used because it shows that 10,000 rows have been retrieved. We will try again, this time with CHILD_NUMBER 1:

    
    SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('c7su63uw7nch6',1,'ALLSTATS LAST'));
    
    SQL_ID  c7su63uw7nch6, child number 1
    -------------------------------------
    SELECT /*+ GATHER_PLAN_STATISTICS */   C1,   C2 FROM   T1 WHERE   C2 =
    :N1
    
    Plan hash value: 3617692013
    
    ------------------------------------------------------------------------------------
    | Id  | Operation         | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
    ------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |      |      1 |        |    990K|00:00:00.83 |     108K|
    |*  1 |  TABLE ACCESS FULL| T1   |      1 |    988K|    990K|00:00:00.83 |     108K|
    ------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       1 - filter("C2"=:N1)
    

    The foregoing shows the actual plan that has been used. Sharing the adjustment slider (first available with Oracle Database 11.1) is reached and forced to re-evaluate the execution plan to avoid a very slow recovery through the index - which won't happen before 11.1 database Oracle.

    Just to illustrate:

    ALTER SESSION SET OPTIMIZER_FEATURES_ENABLE='10.2.0.4';
    
    VARIABLE N1 NUMBER
    EXEC :N1:=2
    
    SELECT /*+ GATHER_PLAN_STATISTICS */
      C1,
      C2
    FROM
      T1
    WHERE
      C2 = :N1;
    
    no rows selected
    
    Elapsed: 00:00:00.00
    
    SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'ALLSTATS LAST'));
    
    SQL_ID  c7su63uw7nch6, child number 2
    -------------------------------------
    SELECT /*+ GATHER_PLAN_STATISTICS */   C1,   C2 FROM   T1 WHERE   C2 =
    :N1
    
    Plan hash value: 236868917
    
    ---------------------------------------------------------------------------------------------------
    | Id  | Operation                   | Name      | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
    ---------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT            |           |      1 |        |      0 |00:00:00.01 |       3 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| T1        |      1 |   5957 |      0 |00:00:00.01 |       3 |
    |*  2 |   INDEX RANGE SCAN          | IND_T1_C2 |      1 |   5957 |      0 |00:00:00.01 |       3 |
    ---------------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       2 - access("C2"=:N1)
    

    Note in the above that the CHILD_NUMBER is now 2.

    Continues:

    EXEC :N1:=99
    SET TIMING ON
    
    SELECT /*+ GATHER_PLAN_STATISTICS */
      C1,
      C2
    FROM
      T1
    WHERE
      C2 = :N1;
    
    10000 rows selected.
    
    Elapsed: 00:00:05.31
    
    SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'ALLSTATS LAST'));
    
    SQL_ID  c7su63uw7nch6, child number 2
    -------------------------------------
    SELECT /*+ GATHER_PLAN_STATISTICS */   C1,   C2 FROM   T1 WHERE   C2 =
    :N1
    
    Plan hash value: 236868917
    
    ---------------------------------------------------------------------------------------------------
    | Id  | Operation                   | Name      | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
    ---------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT            |           |      1 |        |  10000 |00:00:00.02 |    1783 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| T1        |      1 |   5957 |  10000 |00:00:00.02 |    1783 |
    |*  2 |   INDEX RANGE SCAN          | IND_T1_C2 |      1 |   5957 |  10000 |00:00:00.01 |     690 |
    ---------------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       2 - access("C2"=:N1)
    

    The CHILD_NUMBER is always 2.

    Continues:

    EXEC :N1:=1
    
    SET AUTOTRACE TRACEONLY STATISTICS
    
    SELECT /*+ GATHER_PLAN_STATISTICS */
      C1,
      C2
    FROM
      T1
    WHERE
      C2 = :N1;
    
    990000 rows selected.
    
    Elapsed: 00:00:16.91
    
    Statistics
    ---------------------------------------------------
              0  recursive calls
              0  db block gets
         175927  consistent gets
              0  physical reads
              0  redo size
       21958348  bytes sent via SQL*Net to client
         726508  bytes received via SQL*Net from client
          66001  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
         990000  rows processed
    
    SET AUTOTRACE OFF
    
    SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('c7su63uw7nch6',2,'ALLSTATS LAST'));
    
    SQL_ID  c7su63uw7nch6, child number 2
    -------------------------------------
    SELECT /*+ GATHER_PLAN_STATISTICS */   C1,   C2 FROM   T1 WHERE   C2 =
    :N1
    
    Plan hash value: 236868917
    
    ---------------------------------------------------------------------------------------------------
    | Id  | Operation                   | Name      | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
    ---------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT            |           |      1 |        |    990K|00:00:01.63 |     175K|
    |   1 |  TABLE ACCESS BY INDEX ROWID| T1        |      1 |   5957 |    990K|00:00:01.63 |     175K|
    |*  2 |   INDEX RANGE SCAN          | IND_T1_C2 |      1 |   5957 |    990K|00:00:00.68 |   67932 |
    ---------------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       2 - access("C2"=:N1)
    

    The foregoing is the execution plan for CHILD_NUMBER 2 - notice that this time he reports 990 000 recovered lines, it's the execution that was used - adaptive cursor sharing plan do not have take effect and force the re-evaluation of the implementation plan - implementation plan has NOT been changed for a full table scan. That's the risk you take if you allow histograms on columns that have an uneven distribution of values and bind variables are used in the WHERE clause that refers to the column.

    Charles Hooper
    Co-author of "Expert Oracle practices: Oracle Database Administration of the Oak Table.
    http://hoopercharles.WordPress.com/
    IT Manager/Oracle DBA
    K & M-making Machine, Inc.

  • Estimate of poor cardinality using Bind Variables

    Hi I'm using the 11.2.0.4.0 Oracle version. I have a query that is underway for the plan of the poor execution by the estimate of poor cardinality for two tables (I've extracted and published this part only) as I mentioned below, the individual conditions for which the estimate goes bad and moving entire query execution path.

    These are for two tables and currently we use BIND variable for them in our code, and I notice, its best estimate gives with literals. I need to know how to handle this scenario that I need this query to execute for all types of volumes. Is there something I can do without changing the code, as it works well for most of the execution? In the current scenario of the main query that uses those below tables providing a plan (index + nested loop) that works very well for small volume, but running for 10 hr + for large volume as ideally its going to the same regime.
    And Yes, most time that this request will be hit for small volume, but killing some appearance of large volume presents the performance of the queries.


    Here are the values of the variable binding.

    B1 VARIABLE VARCHAR2 (32);
    B2 VARIABLE VARCHAR2 (32);
    B3 VARIABLE NUMBER;
    B4 VARIABLE VARCHAR2 (32);
    B7 VARIABLE VARCHAR2 (32);
    B5 VARIABLE NUMBER;
    B6 VARIABLE NUMBER;

    EXEC: B1: = 'NONE ';
    EXEC: B2: = NULL;
    EXEC: B3: = 0;
    EXEC: B4: = NULL;
    EXEC: B7: = NULL;
    EXEC: B5: = 0;
    EXEC: B6: = 0;

    ---- For  TABLE1-------
     -- Published Actual VS Etimated cardinality
     
     
    -- With bind values
    select * from TABLE1 SF
    WHERE (   (SF.C1_IDCODE = :B4) OR (NVL (:B4, 'NONE') = 'NONE'))
        AND ( (SF.C2_ID = :B3) OR (NVL (:B3, 0) = 0));
    Plan hash value: 2590266031
    -----------------------------------------------------------------------------------------------------------------------------------------------
    | Id  | Operation                 | Name                | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |  OMem |  1Mem | Used-Mem |
    -----------------------------------------------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT          |                     |      1 |        |  28835 |00:00:00.08 |    2748 |     46 |       |       |          |
    |*  1 |  TABLE ACCESS STORAGE FULL| TABLE1              |      1 |     11 |  28835 |00:00:00.08 |    2748 |     46 |  1025K|  1025K|          |
    -----------------------------------------------------------------------------------------------------------------------------------------------
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       1 - storage((("SF"."C1_IDCODE"=:B4 OR NVL(:B4,'NONE')='NONE') AND ("SF"."C2_ID"=:B3 OR NVL(:B3,0)=0)))
           filter((("SF"."C1_IDCODE"=:B4 OR NVL(:B4,'NONE')='NONE') AND ("SF"."C2_ID"=:B3 OR NVL(:B3,0)=0))) 
     
    -- With literals 
    select * from TABLE1 SF
     WHERE  (   (SF.C1_IDCODE = null) OR (NVL (null, 'NONE') = 'NONE'))
          AND ( (SF.C2_ID = 0) OR (NVL (0, 0) = 0));
       Plan hash value: 2590266031
    --------------------------------------------------------------------------------------------------------------------------------------
    | Id  | Operation                 | Name                | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
    --------------------------------------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT          |                     |      1 |        |  28835 |00:00:00.03 |    2748 |       |       |          |
    |   1 |  TABLE ACCESS STORAGE FULL| TABLE1              |      1 |  28835 |  28835 |00:00:00.03 |    2748 |  1025K|  1025K|          |
    --------------------------------------------------------------------------------------------------------------------------------------
    
    --------For TABLE2 ----------------------- 
    -- Published Autotrace plan, as it was taking long time for completion, and actual cardinality is 45M, but its estimating 49 With bind value---
    
    --withbind value
    select * from TABLE2 MTF
    WHERE (   (MTF.C6_CODE = TRIM (:B2)) OR (NVL (:B2, 'NONE') = 'NONE'))
      AND (   (MTF.C3_CODE = :B1)  OR (NVL (:B1, 'NONE') = 'NONE'))
      AND (   (MTF.C4_CODE = :B7)  OR (:B7 IS NULL))
      AND (   (MTF.C5_AMT <= :B6)  OR (NVL (:B6, 0) = 0))
      AND (   (MTF.C5_AMT >= :B5)  OR (NVL (:B5, 0) = 0));
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 1536592532
    -----------------------------------------------------------------------------------------------------------
    | Id  | Operation                  | Name         | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
    -----------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT           |              |    49 | 10437 |   358K  (1)| 01:11:43 |       |    |
    |   1 |  PARTITION RANGE ALL       |              |    49 | 10437 |   358K  (1)| 01:11:43 |     1 |  2 |
    |*  2 |   TABLE ACCESS STORAGE FULL| TABLE2       |    49 | 10437 |   358K  (1)| 01:11:43 |     1 |  2 |
    -----------------------------------------------------------------------------------------------------------
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       2 - storage(("MTF"."C4_CODE"=:B7 OR :B7 IS NULL) AND ("MTF"."C3_CODE"=:B1 OR
                  NVL(:B1,'NONE')='NONE') AND ("MTF"."C5_AMT"<=TO_NUMBER(:B6) OR NVL(:B6,0)=0) AND
                  ("MTF"."C5_AMT">=TO_NUMBER(:B5) OR NVL(:B5,0)=0) AND ("MTF"."C6_CODE"=TRIM(:B2) OR
                  NVL(:B2,'NONE')='NONE'))
           filter(("MTF"."C4_CODE"=:B7 OR :B7 IS NULL) AND ("MTF"."C3_CODE"=:B1 OR
                  NVL(:B1,'NONE')='NONE') AND ("MTF"."C5_AMT"<=TO_NUMBER(:B6) OR NVL(:B6,0)=0) AND
                  ("MTF"."C5_AMT">=TO_NUMBER(:B5) OR NVL(:B5,0)=0) AND ("MTF"."C6_CODE"=TRIM(:B2) OR
                  NVL(:B2,'NONE')='NONE'))
      
    -- with literal
    select * from TABLE2 MTF
    WHERE (   (MTF.C6_CODE = TRIM (null)) OR (NVL (null, 'NONE') = 'NONE'))
     AND (   (MTF.C3_CODE = 'NONE') OR (NVL ('NONE', 'NONE') = 'NONE'))
      AND (   (MTF.C4_CODE = null)  OR (null IS NULL))
       AND (   (MTF.C5_AMT <= 0)  OR (NVL (0, 0) = 0))
      AND (   (MTF.C5_AMT >= 0)  OR (NVL (0, 0) = 0));
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 1536592532
    -----------------------------------------------------------------------------------------------------------
    | Id  | Operation                  | Name         | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
    -----------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT           |              |    45M|  9151M|   358K  (1)| 01:11:41 |       |    |
    |   1 |  PARTITION RANGE ALL       |              |    45M|  9151M|   358K  (1)| 01:11:41 |     1 |  2 |
    |   2 |   TABLE ACCESS STORAGE FULL| TABLE2 |    45M|  9151M|   358K  (1)| 01:11:41 |     1 |  2 |
    -----------------------------------------------------------------------------------------------------------
    
    select column_name,num_nulls,num_distinct,density
    from dba_tab_col_statistics where table_name='TABLE2'
    and column_name in ('C3_CODE','C4_CODE','C5_AMT','C6_CODE');
    C3_CODE 0 65 0.0153846153846154
    C4_CODE 0 2 0.5
    C5_AMT 0 21544 4.64166357222429E-5
    C6_CODE 1889955 71 0.0140845070422535
    
    

    933257 wrote:

    ((SF. C1_IDCODE =: B4) OR (NVL (: B4, 'NONE') = 'NONE'))

    In fact for literals, I did not find any section of the predicate after running the sql code with activation "set autotrace traceonly explain."

    The main problem is with another large query whose cardinality is underestimated due to the presence of these table (table1, table2) with the above mentioned clause, and the query is for the analysis of index + nested with values of Bind loops and take 10 hr +, whereas with literals, its completion in ~ 8minutes with FTS + Hash Join.

    Your real problem is that you try to have just a single SQL query handle all POSSIBLE thanks to the use of embedded FILTERS ' either / or ' filters in the WHERE clause.  You want only a select this OPTION to run whatever filters have been selected at run time by the user or the application using it.  And it would never work.  You really need to SELECT different queries for different combinations of filter conditions.

    Why?  Think for a minute.  How Oracle works internally?  A SQL SELECT query gets analyzed and an execution plan is produced which is stored in the library cache and gets REUSED on all subsequent executions of this query - except in certain cases where there may exist several plans run through several cursors of the child.  So with only SELECT a query you only AN execution plan in the library cache, to be used by all THE executions of this query, regardless of the value of your run-time binding variables.

    Lets put another way - each library cache execution plan is associated with a SQL statement.  If you want a DIFFERENT execution plan then you need run a DIFFERENT SQL statement.  That's how you get a different execution plan - by running a different SQL statement.  Running the SAME SQL query generally you will get the SAME execution plan every time.

    In addition, because of the "either / or" filters that you use you will end up generally with a full Table Scan on each of the referenced tables.  Why?  Given that the optimizer must produce an implementation plan that manages all possible contingencies for all values of possible bind variables in the SELECT.  If the optimizer should choose to use any index based on one of these "either / or" filters then it would only help performance when real value was provided, but it would be really bad if a NULL value was supplied.  If the optimizer ends up ignoring the index because they are not always optimal for all possible input values and instead chose a plan that is "good enough" for all input values possible.  That means that it will use a scanning Table full.

    I hope you can see that it is precisely what is happening for you with your query.  You select this OPTION to manage the different combinations of filter, which leads to the execution plan only one, which leads to scans full Table on the referenced tables in these ' either / or ' filters.

    The solution?  Build queries SELECT DIFFERENT when input values are NULL.  How you do that?  Read this article to ask Tom that tells you:

    http://www.Oracle.com/technetwork/issue-archive/2009/09-Jul/o49asktom-090487.html

    To sum up - when you have real value for a bind variable 'bind_var1' add the following filter to your CHOICE:

    AND column_name1 =: bind_var1

    When the binding variable is NULL, add the filter according to your CHOICE:

    AND (1 = 1 OR: bind_var1 IS NULL)

    Now, you'll have 2 queries SELECT must be performed, which have exactly the same number of variables in the same order bind, which is important.  When you then run one of these variations, Oracle can analyze and optimize each one SEPARATELY, with a single execution by the SELECT query plan.

    When you provide a real value, the filter is a normal 'column = value' that the optimizer can use all indexes on this column, because NULL values are not referenced.

    When there is no real value, the optimizer will analyze the '1 = 1 GOLD' and realize that "1 = 1" is set to TRUE and GOLD, it is quite TRUE regardless because the binding variable is null or not.  This means that the optimizer will actually REMOVE this filter, because it filters nothing because it is always TRUE.  You will end up with an operating plan based on the other filters in the query, which is what you want because you have no filter on this column.

    What is it - producing distinct SELECT queries to determine if you have a real value to filter or not you end up with DIFFERENT execution plans for each of them, and each of them is OPTIMAL for this particular set of filters.  Now you get good performance for each variation of the performance of the SELECTION, rather than sometimes good and sometimes very bad when using SELECT only one.  It is impossible to try to get multiple shots of execution 'optimal' out of a SELECT query.  That's why you get mediocre performance under different bound the values of the variables.

    John Brady

  • Estimates of cardinality for index range scan with bind variables

    Oracle 11.2.0.4

    I am struggling to explain that the cardinality estimates for a scan of the index systematic range when using the bind variable.

    Consider the following query:

    SELECT /*+ INDEX(t1) */ *
    FROM   t1
    WHERE  source_id <= ?;
    
    

    Cardinalities for the INDEX RANGE SCAN and ACCESS of the TABLE are the same for different literal predicates, for example, source_id < = 5:

    ------------------------------------------------------------------------------------
    | Id  | Operation                   | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    ------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT            |      |    50 |   350 |    12   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| T1   |    50 |   350 |    12   (0)| 00:00:01 |
    |*  2 |   INDEX RANGE SCAN          | IX1  |    50 |       |     2   (0)| 00:00:01 |
    ------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       2 - access("SOURCE_ID"<=5)
    
    

    If a variable binding is used instead of a literal, the overall selectivity is 5%. However, why the optimizer based on CSSTidy gives a cardinality estimated 11 for the scan of the index systematic range? As with the predicates literal, surely the cardinalities of the index range scan and access table should be the same?

    ------------------------------------------------------------------------------------
    | Id  | Operation                   | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    ------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT            |      |    50 |   350 |     5   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| T1   |    50 |   350 |     5   (0)| 00:00:01 |
    |*  2 |   INDEX RANGE SCAN          | IX1  |    11 |       |     2   (0)| 00:00:01 |
    ------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       2 - access("SOURCE_ID"<=TO_NUMBER(:A))
    
    

    Unit test code:

    CREATE TABLE t1
    ( id NUMBER
    , source_id NUMBER
    );
    
    CREATE INDEX ix1 ON t1 (source_id);
    
    INSERT INTO t1
    SELECT level
         , ora_hash(level,99)+1
    FROM   dual
    CONNECT BY level <= 1000;
    
    exec DBMS_STATS.GATHER_TABLE_STATS(user,'T1')
    
    EXPLAIN PLAN FOR
    SELECT /*+ INDEX(t1) */ *
    FROM   t1
    WHERE  source_id <= 5;
    SELECT * FROM TABLE(dbms_xplan.display);
    
    EXPLAIN PLAN FOR
    SELECT /*+ INDEX(t1) */ *
    FROM   t1
    WHERE  source_id <= :a;
    SELECT * FROM TABLE(dbms_xplan.display);
    
    

    There are various places where the optimizer uses an assumption, and lie unpeekable (and of Villa "unknowable value") introduced guess.

    For unpeekable binds the conjecture for column<= {unknown}="" is="" 5%="" for="" table="" access="" (hence="" 50="" rows="" out="" of="" 1,000),="" but="" it's="" 0.009="" for="" index_column=""><= {unknown},="" which="" means="" i="" was="" expecting="" to="" see="" 9="" as="" the="" row="" estimate="" on="" the="" index="" range="">

    I just ran some quick tests, and EXPLAIN the PLAN seems to just use 0.011 selectivity in this case (in different versions of Oracle) although if we do the bind variable unpeekable at run time (and sample dynamic block etc.) optimization for execution is 0.009%.

    Concerning

    Jonathan Lewis

    Update: and this is a very old reference to the 0.009 (and 0.0045 for ' between the ' when it is applied to a clue: cost based Oracle - access Chapter 4 single B-tree )

  • Bind Variables on the view objects child definition

    JDeveloper 12 c

    I have an interesting question.  I have a view (AisWebModulePrivsVO) object that uses a bindvariable hardcoded in the view object.

    SELECT * FROM SYS. DBA_ROLE_PRIVS where DEALER =: bvUserName

    If I put this in the request module I can programmatically set the variable binding (view object contains the getter/setter for the binding variable).

    AisWebModulePrivsVOImpl voTest = am.getAisWebModulePrivsVI ();

    voTest.setbvUserName ("ARAWLS");

    voTest.executeQuery ();

    System.out.println ("test reports:" + voTest.getRowCount ());

    and get the correct number of records based on return on the user and database roles. If it works.

    What does NOT work, it is when this display object is a recording of 'the child' to another display object (as specified by a link between the two and by setting the object from a point of view as one child of the other in the module of the application).

    AppModule looks like this:

    CgRefCodesVO

    AisWebModulePrivsVO

    In this case, I have a view called cgRefCodeVO parent object.  There is a link between this and the AisWebModulePrivsVO. The relationship works.

    AISMenuAppModuleImpl m = ADFUtils.getApplicationModuleForDataControl (appMod) (AISMenuAppModuleImpl);

    CgRefCodesVOImpl cgRefVO = am.getCgRefCodes1 ();

    AisWebModulePrivsVOImpl modsVO = am.getAisWebModulePrivs1 ();

    modsVO.setbvDBUserName ("ARAWLS");  This doesn't seem to work here...

    cgRefVO.executeQuery ();

    All lines rs = cgRefVO.getRowSet ();

    RS. Reset();

    While (rs.hasNext ()) {}

    CgRefCodesVORowImpl CgRefRow = rs.next ((CgRefCodesVORowImpl));

    System.out.println ("RvLowValue:" + CgRefRow.getAttribute ("RvLowValue"));

    RowIterator aisModulesRowIter = CgRefRow.getAisWebModulePrivs ();

    System.out.println ("number of children:" + aisModulesRowIter.getRowCount ());

    }

    The code compiles and runs.  She translates: "County of the child: 0" for each row returned by the

    I also tried:

    VM VariableValueManager = modsVO.ensureVariableManager ();

    vm.setVariableValue ("bvDBUserName", "ARAWLS");

    And:

    modsVO.setNamedWhereClauseParam ("bvDBUserName", "ARAWLS");

    and (of https://community.oracle.com/message/2746551#2746551 ( )

    @Override

    protected void executeQueryForCollection (Object, object, Object [] object2, int i) {}

    I coded everything hard to see if it would work.

    setNamedWhereClauseParam ("bvDBUserName", "ARAWLS");

    super.executeQueryForCollection (object, object2, i);

    }

    and from there, I changed around the order to execute the objects in view.    https://community.oracle.com/message/10632314#10632314

    How can I set the variable binding in the object view child?

    Thank you

    Stuart

    It is an interesting example how ADF works up to a certain point... then you need to start to make cuts to modify its behavior as you wish.

    I decided that it was just easier to delete 'the child' parent (in the app module) and create a view of criteria on this subject for what would basically query the records to get the same results, like a child.

    Perhaps not as elegant, but so much easier.

    Thanks for your help.

    Stuart

  • Passing literal as a bind variable in Jena

    Hi all

    We are able to use bind variable with the id RDFVID. However, if we want to check for a literal using the jena/joseki adapter we are not able to perform the same operation. In our case the literal is of type string.

    We use:

    * WLS 12.1.0.3.

    * Adapter Jena 2.11.1.

    * Joseki 3.4.4.

    * ARQ 2.9.2.

    We try to run the following query on the stop of joseki.

    PREFIX foaf: http://xmlns.com/FOAF/0.1/ >

    PREFIX vcard: http://www.w3.org/2001/vCard-RDF/3.0# >

    PREFIX xsd: http://www.w3.org/2001/XMLSchema# >

    PREFIX oext: < http://Oracle.com/Semtech/Jena-adaptor/ext/function# >

    PREFIX ORACLE_SEM_FS_NS: http://Oracle.com/Semtech#no_fall_back, ALLOW_DUP = T, s2s >

    PREFIX ORACLE_SEM_HT_NS: http://Oracle.com/Semtech#monitor >

    PREFIX ORACLE_SEM_UEAP_NS: http://oracle.com/semtech#f$ RDFVID % 20 (ORACLE_ORARDF_RES2VID(?)) >

    PREFIX ORACLE_SEM_UEPJ_NS: http://Oracle.com/Semtech#f $RDFVID >

    PREFIX ORACLE_SEM_UEBV_NS: http://Oracle.com/Semtech# 'Lee' >

    SELECT? f

    WHERE

    {graph? g {? vcard:N p? vn.}}

    ? VN vcard:Family? f.

    ? FOAF:title p 'Sen'.}

    }

    The result set is empty. Replace the line 'ORACLE_SEM_UEBV_NS PREFIX: <http://oracle.com/semtech# "Lee"> "by any Variant is to be given an error or the empty set. (Some variants (& quot;)) Lee & quot; Lee, "Lee" & #39; Lee & #39; )

    When we use the generated SQL code of jena/joseki, we are able to get the right result if we execute the query even in SQLDeveloper. To use the variable binding is "Lee" (the binding variable is the price included). This will give you a correct result in withdrawal.

    Kind regards

    Max

    Hi Max,.

    In the area of the UEBV, you must put "Lee" encoded in URL string

    Please, try the following and see if it helps.

    % 22Lee % 22

    Thank you

    Zhe Wu

  • SP2-0552: Bind variable

    Hello

    I get this error during the execution of the following:

    SQL > DECLARE

    2

    V_task VARIABLE 3: = "tache_338";

    4

    5 BEGIN

    (6) DBMS_SQLPA.execute_analysis_task

    TaskName 7 = >: v_task,.

    8 execution_type = > "test run."

    9 execution_name = > 'after_change');

    10 END;

    11.

    SP2-0552: Bind "V_TASK" variable not declared.

    How should I write this code?

    Thank you.

    Hello.

    Note:

    The use of Bind Variables

    Bind variables are variables that you create in SQL * Plus and then the reference in PL/SQL or SQL. If you create a variable binding in SQL * Plus, you can use the variable as you would for a variable declared in your PL/SQL subprogram and access the variable from SQL * more. You can use bind variables for things such as the storage of return codes or debug your PL/SQL subprograms.

    DECLARE

    2

    3 v_task vachar2 (32): = "tache_338";

    4

    5 BEGIN

    (6) DBMS_SQLPA.execute_analysis_task

    Task_Name 7-online v_task,

    execution_type 8-online 'run test ',.

    9 execution_name-online 'after_change');

    10 END;

    11.

    See the link: DBMS_SQLPA

    Scripts SQL help * more

  • Procedure of the ODI - Bind Variables in the conditional statements

    In an ODI procedure, is it possible to add a conditional statement around bind variable?  For example, if I use OdiOutFile as the command on the target and Oracle as the command on the Source, I can use bind variables to fill the data in this table (we are in fact using OdiInvokeWebService and passing a XML structure very complex, but it's easier for testing):

    OdiOutFile-FILE = C: / TEST. TXT

    < person >

    < first > #FIRSTNAME < / first >

    < Middle > #MIDDLENAME < / Middle >

    < last > #LASTNAME < / last >

    < / person >

    It is a way to add a conditional statement using one of these bind variables?  For example, if I wanted to test #MIDDLENAME null and null value, produced this portion of XML, such as:

    OdiOutFile-FILE = C: / TEST. TXT

    < person >

    < first > #FIRSTNAME < / first >

    < % if (#MIDDLENAME! = null) {% >}

    < Middle > #MIDDLENAME < / Middle >

    < %} % >

    < last > #LASTNAME < / last >

    < / person >

    This type of scriptlet syntax seems to work fine, as long as the binding variable is not in the mix (if I put "true is true" or "true == false" in the case, it shows or does not show this line in the XML file, as expected).  I use ODI 11.1.1.7.0

    ----------------------

    Post edited by: KevinPratt

    Thanks for the reply!  As suggested, in what follows, I changed the command and it works as you want

    OdiOutFile-FILE = C: / TEST. TXT-APPEND

    < person >

    < first > #FIRSTNAME < / first >

    < @ if (!) (("#MIDDLENAME".equals(""))) {@ >}

    < Middle > #MIDDLENAME < / Middle >

    {< @} @ >

    < last > #LASTNAME < / last >

    < / person >

    Using the syntax <@ ...="" @="">serve your purpose. Also, I think you might want to include the variable binding in the quotation to avoid syntax errors.

  • Bind Variables in SPARQL queries

    Hello

    I am interested in bind variable and use between requests, but if I understand the concept that underlies the example given in the documentation (http://docs.oracle.com/cd/E11882_01/appdev.112/e25609/sem_jena.htm#autoId15) I'm not able to grasp how exactly to the results of a query_1 for a ? x variable to another query_2query.

    Would it not possible to provide a more detailed explanation and a simple code working on how to do it using Jena Adapater.

    Thank you.

    Sorry for the late reply. Was tied up with a date limit.

    Here is a complete example.

    (1) assumes a model with the following triplets

    % test/forum2550843.nt cat

    .

    .

      .

    .

    .

    (2) run the following query to obtain the values of bind and internal ID resources

    % Cat./tests/forum2550843.sparql1

    PREFIX oext: http://oracle.com/semtech/jena-adaptor/ext/function#>

    Select? x (oext:build - uri-for-id(?x) as? xid)

    where {? y ? x}

    Here is a possible answer to the query above.

    soln (? xid = <> 6354524810780932050>) (? x = )

    (3) to say we want to push the values to bind to? x from the above query in

    "SELECT? topic? "x WHERE {? subject? x}"

    We can build and run the following query:

    % Cat./tests/forum2550843.sparql2

    PREFIX ORACLE_SEM_FS_NS: http://oracle.com/semtech#no_fall_back>

    PREFIX ORACLE_SEM_UEAP_NS: http://oracle.com/semtech#x$ RDFVID % 20 in(?) >

    PREFIX ORACLE_SEM_UEPJ_NS: http://oracle.com/semtech#x $RDFVID>

    PREFIX ORACLE_SEM_UEBV_NS: http://oracle.com/semtech#6354524810780932050>

    SELECT? topic? x

    WHERE {}

    ? topic ? x

    }

    Here are the answers:

    soln (? x = ) (? subject = )

    soln (? x = ) (? subject = )

    It will be useful,

    Zhe Wu

  • Bind Variables out of use

    I don't know where to post this, please point me in the right direction if necessary. It seems that binds the variables in the order that they appear in the SQL statement works very well, but bind them down causes the statement to be poorly executed. Here is the result on my machine, Win 7 Pro, JDK 1.7.0_21 against Oracle 11.2.0.3 on Linux 64.

    Create table test
    Insertion of temporary data
    Display all data
    10 50 Red
    20 40 Green
    30 30 Blue
    10 40 Orange
    20 50 yellow
    70 rose 10
    Selection of A = 10 and B = 50 in the order
    10 50 Red
    Selection in the reverse of A = 10 and B = 50
    Table of drop test

    Here's the code that produced these results. The difference between the declarations of the first and the second selection is simply the order, that the variables are related. Am I doing this wrong?

    Import oracle.jdbc.OracleDriver;

    to import java.sql.CallableStatement;
    import java.sql.Connection;
    to import java.sql.DriverManager;
    import java.sql.ResultSet;

    public class RunTests {}
    Private Sub log (String s) {}
    System.out.println (s);
    }

    Private Sub log (row of the ResultSet) throws Exception {}
    log (Row.GetString ("a") + "\t" + row.getString ('b') + '\t' + row.getString ("c"));
    }

    Private Sub start (String [] args) throws Exception {}
    DriverManager.registerDriver new (OracleDriver());
    Connection c is DriverManager.getConnection ("jdbc:oracle:thin:@someserver:1521:sid", "user", "pass");.

    Log ('Creating test table');
    CallableStatement cs = c.prepareCall ("create table eric_example (number, number of b, c varchar2 (30))");
    DSI Execute();
    DSI Close();

    Journal ("insertion of temporary data");
    CS = c.prepareCall ("insert into eric_example values (:,: b: c) '");
    cs.setInt (": a", 10);
    cs.setInt (": b", 50);
    cs.setString (": c", "Red");
    cs.executeUpdate ();

    cs.setInt (": a", 20);
    cs.setInt (": b", 40);
    cs.setString (": c", "Green");
    cs.executeUpdate ();

    cs.setInt (": a", 30);
    cs.setInt (": b", 30);
    cs.setString (": c", "Blue");
    cs.executeUpdate ();

    cs.setInt (": a", 10);
    cs.setInt (": b", 40);
    cs.setString (": c", "Orange");
    cs.executeUpdate ();

    cs.setInt (": a", 20);
    cs.setInt (": b", 50);
    cs.setString (": c", "Yellow");
    cs.executeUpdate ();

    cs.setInt (": a", 70);
    cs.setInt (": b", 10);
    cs.setString (": c", "Pink");
    cs.executeUpdate ();
    DSI Close();

    Log ("indicating all data");
    CS = c.prepareCall ("select * from eric_example");
    ResultSet rs = cs.executeQuery ();
    While (RS. Next {}
    log (RS);
    }
    RS. Close();
    DSI Close();

    Log ('Selecting A = 10 and B = 50 in the order");
    CS = c.prepareCall ("select * from eric_example where a =: a and b =: b");
    cs.setInt ("a", 10);
    cs.setInt ("b", 50);
    RS = cs.executeQuery ();
    While (RS. Next {}
    log (RS);
    }
    RS. Close();
    DSI Close();

    Journal ("selection A = 10 and B = 50 in reverse");
    CS = c.prepareCall ("select * from eric_example where a =: a and b =: b");
    cs.setInt ("b", 50);
    cs.setInt ("a", 10);
    RS = cs.executeQuery ();
    While (RS. Next {}
    log (RS);
    }
    RS. Close();
    DSI Close();

    Log ("table Dropping test");
    CS = c.prepareCall ("drop table eric_example");
    DSI Execute();
    DSI Close();

    c.Close ();
    }

    Public Shared Sub main (String [] args) {}
    RunTests runTests = new RunTests();
    try {}
    runTests.start (args);
    } catch (Exception e) {}
    e.printStackTrace ();
    System.Exit (1);
    }
    }

    }

    >
    I don't know where to post this, please point me in the right direction if necessary. It seems that binds the variables in the order that they appear in the SQL statement works very well, but bind them down causes the statement to be poorly executed. Here is the result on my machine, Win 7 Pro, JDK 1.7.0_21 against Oracle 11.2.0.3 on Linux 64.
    >
    Although what do you expect to link which includes a colon in the name?

    cs.setInt(":a", 10);
    

    And the code like this

    cs.setInt("a", 10);
    

    is NOT supported. You can not bind by name using the setXXX methods.

    Change your code to use syntax supported, and you shouldn't have a problem.

    cs = c.prepareCall("select * from eric_example where a = :1 and b = :2");
    cs.setInt(1, 10);
    cs.setInt(2, 50);
    

    See "Binding parameters named" in the Guide of Dev JDBC
    http://docs.Oracle.com/CD/B28359_01/Java.111/b31224/apxref.htm#BABEJDBH
    >
    Binding parameters named

    Binding by name is not supported when using the setXXX methods. In certain circumstances, previous versions of the Oracle JDBC drivers have allowed binding variables name declaration when using the setXXX methods. In the following statement, the variable named EmpId is linked to the whole 314159.

    P = conn.prepareStatement PreparedStatement
    ("" SELECT name FROM emp WHERE id =: EmpId "");
    p.setInt (1, 314159);

    This ability to set by name using the setXXX methods is not part of the specification, JDBC and Oracle does not support. JDBC drivers can throw an SQLException exception or produce unexpected results. From JDBC Oracle Database 10 g drivers, connection by name is supported by using the setXXXAtName methods.
    >
    You can use one of these 'previous versions' driver which seem to confirm the connection by name but these links are NOT supported, so you should not try to use them.

    There is no work around. Replace the names of variables bind with an exclamation mark.

  • How to pass a list as a bind variable in SQL Developer?

    How can I pass a list as a bind variable in SQL Developer?

    The following query in SQL Developer so work I put ": prmRegionID = 2.

    SELECT COUNTRY_ID,
    COUNTRY_NAME
    OF HUMAN RESOURCES. COUNTRY
    WHERE IN REGION_ID (: prmRegionID);

    The problem is that I can't find how to set ": prmRegionID = 2, 3.

    I know that I can replace ": prmRegionID" by a proxy '& prmRegionID '. The above query will work well with"& prmRegionID = 2" and with "& prmRegionID = 2, 3". "

    But with this solution, I lost all the benefit of the use of bound variables (analysis hard against soft parse, possibility of SQL injection, etc.).

    I'm learning how to do this in SQL, as well as the use of UDT in this thread: How to move a list as a bind variable?

    But with this solution, I've lost nice SQL Developer user interface. In SQL developer, it is easy to test a query using the standard binding variable. When we start the application, a pop up asking for a value of the variable binding.

    With the UDT, the interface request always variable binding standard. You have an idea on how I can get a variable string binding (such as 1, 2, 10) in a set of NUMBER or VARCHAR2? This way I would be able to launch a standard query in SQL Developer to test my application.


    Can someone tell me what is the best approach to this?

    Thank you in advance,


    MB

    Hi Blais,

    Thank you for trying the SQL and PL/SQL instance before coming here - it was definitely the right approach, and you've got some very good suggestions there. Your needs for a invite only bind to the value in the clause list, I think I have a possible solution. I'll introduce you to a list of characters, so you'll have to tweak it for other types of data. First, add the following to your schema:

    create or replace
    TYPE bind_tab_typ AS TABLE OF VARCHAR2(4000);
    
    create or replace
    FUNCTION comma_to_table(iv_raw IN VARCHAR2)
    RETURN bind_tab_typ
    PIPELINED
    IS
       ltab_lname dbms_utility.lname_array;
       ln_len     BINARY_INTEGER;
    BEGIN
       dbms_utility.comma_to_table(list   => iv_raw
                                  ,tablen => ln_len
                                  ,tab    => ltab_lname);
       FOR i IN 1 .. ln_len LOOP
          PIPE ROW (ltab_lname(i));
       END LOOP;
    END;
    

    Now you can write a query, say for scott.dept, as follows, and have executed statement ask the value list in the clause as a single binding variable:

    select *
    from dept
    where dname in (
      select * from table( comma_to_table( :BNDS ))
    );
    

    When you are prompted, provide the list of values separated by a single comma without any extra spaces.

    I don't know if the Varchar2 (4000) really needs to be which is great. I use it because that's what dbms_utility.lname_array uses.

    Kind regards
    Gary
    SQL development team

  • Bind variables in Plsql

    Since plsql automatically converts the variables in the where clause and the clause values in bind variable. So what is the difference between the two statements below?


    create table t (number);

    1)
    declare
    x number: = 109;
    Start
    Insert values into t (x); - plsql will make this variable as a variable binding, and provide the value of x in run mode.
    exception when others then
    dbms_output.put_line (SQLERRM);
    end;


    2)
    declare
    x number: = 109;
    Start
    immediately execute "insert into values t (: B1)" using x. -the user has explicitly used bind variable.
    dbms_output.put_line (SQLERRM);
    end;

    [EDIT: after further testing, I redesigned my original explanation because Rahul referenced below.]

    Rahul K wrote:
    1)
    declare
    x number: = 109;
    Start
    Insert values into t (x); - plsql will make this variable as a variable binding, and provide the value of x in run mode.
    exception when others then
    dbms_output.put_line (SQLERRM);
    end;

    2)
    declare
    x number: = 109;
    Start
    immediately execute "insert into values t (: B1)" using x. -the user has explicitly used bind variable.
    dbms_output.put_line (SQLERRM);
    end;

    ((1) SQL static, 2) is the dynamic SQL code. Static SQL is parsed to less than dynamic SQL statements.

    As you make these calls in PL/SQL anonymous blocks and not in the stored procedures, there will be some analysis every time. Analysis of the static SQL will do less work than the analysis of the dynamic SQL, but both will avoid "hard" analysis which adds the execution plan to the shared pool.

    Published by: Ashton stew on January 6, 2013 10:16

  • How to get SQL that do not use bind variables

    Hello

    I am trying to identify the SQL code which should benefit from the use of bind variables.
    First of all, I tried to get the common signature of all the sql calls, using:
    select * from (
    select  force_matching_signature, count(1) from v$sql where force_matching_signature<>0 group by force_matching_signature order by 2 desc
    ) where rownum < 50;
    Then I copied these values to the Clipboard and run:
    select sql_text from v$sql where force_matching_signature=<<<copied_signature_value>>>;
    Now, I want to make it automatically and get only 1 occurrence of each SQL that resembles others by using a query.

    I tried this:
    select sql_text from
    (
    select sql_text, force_matching_signature, row_number() over (partition by force_matching_signature order by sql_text desc)rn from v$sql where force_matching_signature <>0
    )where rn <2 and rownum < 10 order by force_matching_signature desc
    But it is not returning results by showing up at the count (1) from the first query, I've used. How can I change this if I get the results in order of "importance"?




    Thank you

    And I said. First use the command by then use rownum. I did not mention row_number. Also, there should be no need to add more columns.
    Have you tried it? Why it did not work?

    example not tested

    select * from (
       select sql_text from (select sql_text, force_matching_signature, row_number() over (partition by force_matching_signature order by sql_text desc) rn from v$sql where force_matching_signature != 0)
       where rn = 1
       order by force_matching_signature desc /* add any ordering you like here */
       )
    where rownum < 10  /* then filter on the first 10 results */
    

    If you want to order that the statement that found most of the time comes first, say so. However, I don't see how to group in your case.
    Maybe like this

    example of tested

    select * from (
       select cnt, sql_text
       from (select sql_text, force_matching_signature, row_number() over (partition by force_matching_signature order by sql_text desc) rn , count(*) over (partition by force_matching_signature) cnt
             from v$sql
             where force_matching_signature != 0)
       where rn = 1
       order by cnt desc, force_matching_signature desc /* add any ordering you like here */
       )
    where rownum < 10  /* then filter on the first 10 results */
    ;
    

    Published by: Sven w. October 11, 2012 14:49

    Published by: Sven w. October 11, 2012 14:51

    Published by: Sven w. on October 11, 2012 14:56 - number column added to the output

  • Insert with bind variables

    I will execute bind_test.sql with the following parameters to sql-plus. The script runs without error. However, it does not insert a row in the table.


    bind_test. 20100912 1 1001 SQL;


    bind_test. SQL
    ----------------------------------------------------------
    Set serveroutput on

    declare

    X VARCHAR2 (31);
    NUMBER OF Y;
    NUMBER OF Z;

    Start

    dbms_output. Enable (10000);
    X: = "& 1";
    Y: = & 2;
    Z: = & 3;

    Insert in the test
    values (TO_DATE (X, 'YYYYMMDD'), Y, Z, null, null, null);


    commit;


    dbms_output.put_line ('var1 = "|") X | "var2 =" | THERE | "var3 = ' |" (Z);

    end;
    /

    ----------------------------------
    Output:
    10 old: X: = "& 1";
    10 new: X: = '20100912';
    old 11: Y: = & 2;
    11 new: Y: = 1;
    12 old: Z: = & 3;
    12 News: Z: = 1001;
    var1 = 20100912 var2 = 1 var3 = 1001

    PL/SQL procedure successfully completed.
    ---------------------------------------

    Select * from test;

    no selected line


    Why it is not insert a line? What could be the reason?

    Thanks in advance.
    Amit

    user5922214 wrote:

    I have a big script with multiple INSERT and DELETE statements. I have been asked by DBA to use bind variables rather than direct assignment parameter values in which the condition.

    Impossible.

    Use of variable bind from a client means:
    (1) customer create a unique SQL cursor with bind variable, for example INSERT INTO FOOTABLE VALUES (: 1,: 2: 3)
    (2) customer from a loop of data read
    2.1) reading the customer data (from file, network, device, whatever)
    customer liaison variables 2.2) cursor (assigning values to 1 to 3 SQL cursor variables)
    2.3) client running the cursor
    line insertions 2.4) database
    loop repetitions of 2.5) client for all data
    (3) customer closed the SQL cursor

    This can be done using a SQL script - as a SQL script is not a loop where a single insertion cursor is used. It's a unique script with 100 or 1000 of SQL insert statements. Where each insert statement will result in a cursor SQL must be created.

    SQL * also does not support the approach of customer liaison above variable. It is a command line interface very PRIMITIVE and unsuitable for running scripts from loading/data entry.

    In your case - the right approach would be to have the data into a CSV file, and then load this file using SQL * Loader... where this software will use customer approach above (in fact he still does better because it uses in bulk link and link not row as described above).

Maybe you are looking for