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.

Tags: Database

Similar Questions

  • 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.

  • Create the collection of query with bind variable

    Apex 4.0.2

    By Joel Re: Collection with variable binding the apex_collection.create_collection_from_query_b supports queries containing references to the bind variables (: P1_X) but I don't know how to use this feature, the documentation is not an example, just the API signature for the overloaded version has changed.

    If the query contains 2 bind session state variable references (: P1_X and: P1_Y), can someone please show an example of what to spend for the parameters p_names and p_values to the API?

    Thank you
    procedure create_collection_from_query_b(
        --
        -- Create a named collection from the supplied query using bulk operations.  The query will
        -- be parsed as the application owner.  If a collection exists with the same name for the current
        -- user in the same session for the current Flow ID, an application error will be raised.
        --
        -- This procedure uses bulk dynamic SQL to perform the fetch and insert operations into the named
        -- collection.  Two limitations are imposed by this procedure:
        --
        --   1) The MD5 checksum for the member data will not be computed
        --   2) No column value in query p_query can exceed 2,000 bytes
        --
        --
        -- Arguments:
        --     p_collection_name   =  Name of collection.  Maximum length can be
        --                            255 bytes.  Note that collection_names are case-insensitive,
        --                            as the collection name will be converted to upper case
        --     p_query             =  Query to be executed which will populate the members of the
        --                            collection.  If p_query is numeric, it is assumed to be
        --                            a DBMS_SQL cursor.
        -- example(s):
        --     l_query := 'select make, model, caliber from firearms';
        --     apex_collection.create_collection_from_query_b( p_collection_name => 'Firearm', p_query => l_query );
        --
        p_collection_name in varchar2,
        p_query           in varchar2,
        p_names           in wwv_flow_global.vc_arr2,
        p_values          in wwv_flow_global.vc_arr2,
        p_max_row_count   in number default null)
        ;

    HELEN wrote:
    Apex 4.0.2

    By Joel Re: Collection with variable binding the apex_collection.create_collection_from_query_b supports queries containing references to the bind variables (: P1_X) but I don't know how to use this feature, the documentation is not an example, just the API signature for the overloaded version has changed.

    If the query contains 2 bind session state variable references (: P1_X and: P1_Y), can someone please show an example of what to spend for the parameters p_names and p_values to the API?

    Not tried, but guess something like

    apex_collection.create_collection_from_query_b(
        p_collection_name => 'foobar'
      , p_query => 'select f.foo_id, b.bar_id, b.baz from foo f, bar b where f.foo_id = b.foo_id and f.x = to_number(:p1_x) and b.y = :p1_y'
      , p_names => apex_util.string_to_table('p1_x:p1_y')
      , p_values => apex_util.string_to_table(v('p1_x') || ':' || v('p1_y')))
    
  • How many times can I use a variable binding in a query of the VO?

    Is it possible that I can use a variable binding in a query of VO just once?

    I test a query that keeps throwing the Houston-27122 error by pressing the application module tester. I tried to limit the problem to the simplest possible case, and it seems that the error is caused by the fact that I use the same variable bind two or more times.

    When the query looks like this:

    query1.png

    I have run, enter 'x' and it returns me the result.

    When I change to:

    query2.png

    I have run, enter the value

    param.png

    and immediately get the error

    error.png

    The same thing in sqlplus runs without problem:

    sqlplus.png

    My version of JDev is build JDEVADF_11.1.1.9.0_GENERIC_150314.0718.6673

    Please, advise.

    P. S. where can I find documentation queries are supported in your? In particular, I'm interested if I can use WITH the clause. There seems to be some confusion on this matter (see, for example, Oracle SQL WITH clause support - JDeveloper and ADF)

    Thank you.

    It depends on:

    Open your VO in JDev and go the the query page.

    In the lower part, there is a drop down 'link type', it is usually set to "JDBC-positional. In this case, you will need to provide the bind value for each occurrence of the variable binding separately.

    You could change that to "named" (don't remember the exact name). But beware, this will also affect all relatioships master retail to others your. I think that this is not a good idea to change the style of binding to a single VO.

    Good bye

    DPT

  • problem with bind variables in the SQL query view object

    Hi all

    I use JDev 11.1.2.4.0.

    I have a problem with bind variables in the SQL query view object.

    This is my original SQL

    SELECT sum(t.TIME) , t.legertype_id
    FROM LEDGER t
    WHERE t.nctuser_id = '20022' 
          AND to_char(t.insertdate,'YYYYMMDD') in ('20130930','20130929')
    group by t.legertype_id
    

    In my view .xml object query tab, I am writing this

    SELECT sum(t.TIME) , t.legertype_id
    FROM LEDGER t
    WHERE   t.nctuser_id = '20022'
        AND to_char(t.insertdate,'YYYYMMDD') in :dddd
    group by t.legertype_id
    

    Davis here is a variable of Type liaison: String, updatable and necessary.

    I try to deal with Davis as ('20130930 ', ' 20130929') hoping the view object, run as my original SQL.

    But failed. The view object retrieves 0 line after that I run.

    Why?

    Thank you! ('2original SQL0130930', '20130929') ('20130930 ', ' 20130929')

    A variable binding cannot be used as this is why you must use years table. Check decompilation binary ADF: using oracle.jbo.domain.Array with ViewCriteria to see a solution.

    Timo

  • Why bind variables/page items does not work in the report query?

    Hello world

    I am trying to use the page as a bind variable in the query of my report. I checked the box to state of Session and also added my page elements in the LIST of ITEMS in the SESSION STATE.

    but NO RECORD not DISPLAYED IN THE REPORT at RUN TIME.

    to see live please connect to

    http://Apex.Oracle.com/pls/Apex/f?p=52297:LOGIN_DESKTOP:8355343133792

    user name: [email protected]

    password: 123456

    Hello

    I looked at the application.

    Do you want to go to the report page after click on the report button? If it is, your button to submit the page. Then, you need a branch to access this URL. By submitting the page, your value should be set at the session.

    Kofi

  • How to pass variables bind to select query

    Hello
    I use jdeveloper 11.1.1.6.0 version.
    My use case is - I want to change the settings of the select clause dynamically
    For example
    My view of object query is as follows:
        SELECT FN_GET_ALFA_MESSAGE_DETAIL_SEQ(1,2) FROM DUAL
       
    where FN_GET_ALFA_MESSAGE_DETAIL_SEQ is the name of function.
    I want to pass parameters at runtime instead of 1 and 2 as a and b.
    So, how can I pass dynamic parameters (bind variables) in the display object during execution of the select clause.

    Published by: 925410 on January 7, 2013 18:25

    Hello

    You may have two bind variables (note that bind variables are always preceded by a colon in the query) as below:

    SELECT FN_GET_ALFA_MESSAGE_DETAIL_SEQ(:bind_var1,:bind_var2) FROM DUAL
    

    Then, follow the following steps:
    1. create two variables of the same names liaison in the variables section of Bind in the query tab of the VO
    2 2 disposer have a method in the class Impl of Module of the Application and within this method, pass the values of these bind variables as below:

                Impl vo = null;
                vo = get1();
                vo.setNamedWhereClauseParam("bind_var1", );
                vo.setNamedWhereClauseParam("bind_var2", );
                vo.executeQuery();
    

    3 expose this method of the the module interface of the client Application, then the method will be visible in the section data when updating controls.
    4 use the method to run the original Version of data controls you want :)

    Kind regards
    Seddik

  • The use of bind variables in dynamic query created for Ref Cursor

    Hello

    I'm in a situation where there is a Ref cursor to which the query is built execution based on a loop. This is why the number of links would be known until the program runs.
    The application is currently using literals instead of bind variables.

    code snippet of the above is
    strSql: = "select * from emp where 1 = 1 and ().

    loop cursor1
    If cond is true then
    strSql = strSql | "ename = ' |" Cursor1.ColumnName;
    end loop;

    Open cursor2 for strSql;

    How to use links in the example above.

    sb92075 wrote:

    user13019948 wrote:
    Hello

    Here is the code I have my trying to change literal-based link to the base.

    What do you mean by "based bind?

    who, what, how determines the values to be 'bound '?

    He's referring to the coding style. He is currently using concatenated literal, and the goal is to change it to use the bindings.

    If I understand this it is known as method 4 dynamic SQL and requires DBMS_SQL. There are examples autour but they vary according to the type of statement being generated - SELECT statements require column lists to be parsed, unlike the INSERT/UPDATE/DELETE.

    This came up recently on my current project and I hit a demo. Here a table of names and values accepted procedure and had to build these in a single WHERE clause along the lines of

    AND t_names(i) = t_values(i)
    

    for an undetermined number of elements in the array. For this demonstration, I used a table that we called "attribute" (don't ask) which has columns including 'attribute_id' and 'name', and I need to build a query along the lines of

    select description from attribute where attribute_id = :b1 and name = :b2
    

    by the way '1012' and 'ISIN' respectively. (I use a table better and after a CREATE statement for her but I have to rush right now, sorry).

    declare
       k_sql_base        constant varchar2(500) := 'select description from attribute';
    
       t_names           constant varchar2_t := varchar2_t('attribute_id',  'name');
       t_values          constant varchar2_t := varchar2_t('1012',          'ISIN');
    
       l_sql             varchar2(500) := k_sql_base;
       l_rows_fetched    integer := 0;
       l_value           varchar2(4000);
    
       l_cursor_handle   integer;
    
    begin
       -- Construct the SQL statement with column names and bind variables e.g.
       -- 'select description from mars.attribute where attribute_id = :b1 and name = :b2'
       for i in t_names.first .. t_names.last loop
          l_sql := l_sql ||
             case i
                when t_names.first then ' where ' else ' and '
             end ||
             t_names(i) || ' = :b' || i;
       end loop;
    
       dbms_output.put_line('SQL statment = ' || l_sql); 
    
       -- Parse the statement we built above (the remaining steps require a parsed cursor):
       l_cursor_handle := dbms_sql.open_cursor;
       dbms_sql.parse(l_cursor_handle, l_sql, dbms_sql.native);
    
       -- Associate the 1st column of output with variable l_value - required for SELECT statements:
       -- (actually the 3rd param here 'column' seems to be only used to get a datatype, in this case we want a string -
       -- dbms_sql.column_value actually extracts the value into a specified variable, which can be different.
       -- All examples in the documentation pass a local variable without further comment, so not entirely clear what this does other than set the output datatype.)
       dbms_sql.define_column(l_cursor_handle, 1, l_value, 4000);
    
       -- Now go through values array binding actual values to :bn variables in the cursor (similar to USING clause of EXECUTE IMMEDIATE)
       for i in t_values.first .. t_values.last loop
          dbms_sql.bind_variable(l_cursor_handle, ':b'||i, t_values(i));
          dbms_output.put_line('Bound :b'||i || ' as ' || t_values(i));
       end loop;
    
       -- Open the cursor and fetch the result (no loop here because we are expecting a single-row result):
       l_rows_fetched := dbms_sql.execute_and_fetch(l_cursor_handle);
    
       -- 'Returns value of the cursor element for a given position in a cursor'
       -- Copy the value of column 1 to variable l_value (has to match
       -- dbms_sql.column_value(l_cursor_handle, 1, l_value);
       dbms_sql.column_value(l_cursor_handle, 1, l_value);
    
       dbms_output.put_line('Result = ''' || l_value || '''');
    
       dbms_sql.close_cursor(l_cursor_handle);
    end;
    

    Hope that helps...

  • A query on bind variables

    Hello

    I have a procedure that takes the deptno parameter and inserts that records departments of the table emp for the new table.

    This code does not exist in a real system. I was trying to understand that the advantage of linking variable.i he wrote in different ways as below
    Create table emp3 as select * from emp where 1=2;
    
    create or replace procedure test1 (p_deptno in number)as 
    begin
     execute immediate 'insert into emp3
                                   select *
                                   from emp
                                   where deptno=:1'
                                   using p_deptno;
    end;
    The works fine.i code above writes the following much too.

    create or replace procedure test2 (p_deptno in number)as 
    begin
     insert into emp3
          select *
          from emp
          where deptno=p_deptno;
    end;
    the first no matter what performance benefit over the second one that is the right way to do it in a real-world system?
    . I could not do a comparison of performance because I don't have access to a currently oracle instance and did it in workshop SQL that comes with Apex.My apology for this...

    Kind regards
    CKLP

    Published by: CKLP on August 29, 2011 03:21

    CKLP wrote:

    the first no matter what performance benefit over the second one that is the right way to do it in a real-world system?

    Not really. Do not forget that all interpreted SQLs winds up as sliders to the sharing of the database Pool.

    If a cursor exists already for a SQL statement, SQL will reuse this slider existing (soft parser). So in a SQL engine perspective, there is no different between the two methods. These statements will be btw generate the same cursor SQL - code SQL of base and bind variable positions are identical.

    From a PL/SQL's point of view, there is a huge difference.

    The first option may be a little slower. Why? There he uses the immediate execution interface which is not as optimized as the implicit cursor to the PL/SQL interface. You use a "heavier" SQL interface in PL/SQL which results in more work for the PL/SQL engine.

    The second option is compiled in an implicit cursor. The SQL code is known, controlled and checked, and compiled PL/SQL code deals with a known SQL statement. Unlike the first option.

    The second option also means that your code will not fail (because SQL syntax errors, etc.) currently running. If this static SQL statement in your code is no longer valid (due to a table being deleted, used column removed, etc.), then your code becomes invalid. With the first option, the code remains valid - and when it is executed, it will throw an exception that the (dynamic) SQL cannot be analyzed and that it is not valid.

    The first option also introduces the spectrum of a SQL injection. Let's say that there is a way to cause a buffer overflow exploit in the database call that executes this procedure. The SQL itself is a string, it is an easy attack vector as the overflow needs that data of type string to inject executable code - as opposed to the p-code or machine code. (an unlikely scenario, but the basic principle of security remains true)

    Dynamic SQL is unnecessary for 99% of the time in PL/SQL. And 99% of the time, with the help of the dynamic SQL is for the wrong reasons, or ignorance or both.

  • Query report back "No data found" with bind variables

    I put a simple query in the report query:

    Select "bluefish". 'name' as 'name',
    "bluefish". "" primary_flag "as"primary_flag. "
    "bluefish". "" status "than"status. "
    "bluefish". ' ' ID ' as 'ID' of "bluefish" "bluefish" where "bluefish". "" ID "=: P3_XPRINTID

    When I test the query, the data is returned. However, when I try to run the query using the "Test report" button, I get an error 01403 no data found. If I replace the link with an explicit value variable, the report runs.

    Anyone have any ideas as to what is causing this problem? I use the generic report layout, with different types of output. I'M editting the query and set the binding variable before test report (otherwise, the query is not executed).

    Charles

    In fact the report would go - unlike other products of Oracle who complain about a missing binding variable, Apex is not complaining but interprets as a null value. So if you hurt typed the name of your variable, you are never alerted to it.

    Check the Session window to make sure that it is a value - the most common questions are that you named the wrong variable in the query or your item has no value in session state again.

  • Need help with Bind variable in AF LOV query

    Hello

    I have a problem with the binding variable, if I use bind variable in the VO LOV query then my result does not come, if he has do not bind variable it works fine and if I use the variable binding in the LOV search option then it works fine but if I hide the bind variable and set the value in the prepareSesstion AM method the LOV does not return any value. I try to return the values that also all values are also coming, but these values are not the attribute query AF setting.

    Query is:

    Select substr(d.description,0,40) description
    cm_system_users has
    b cm_user_responsibilities,
    cm_responsibility_processes c,
    cm_processes d
    where a.nt_login =: B_NT_LOGIN
    and a.user_id = b.user_id
    and trunc (sysdate) between b.eff_date and nvl (b.exp_date, sysdate + 1)
    and b.RESPONSIBILITY_ID = c.RESPONSIBILITY_ID
    and c.process_type = d.process_type
    and d.enabled_flag = 'Y' order of d.arguments_flag

    FOLLOW the method:

    CmProcessViewImpl vo = getCmProcessView();
    String nt_login = getUserPrincipalName();
    vo.setNamedWhereClauseParam ("B_NT_LOGIN", nt_login.toUpperCase ());
    vo.executeQuery ();

    Can someone help me with this. It is urgent for me.

    It's a bit underdescribed. IIUC, the purpose of the notice is used as a target for a correct view accessor? (Otherwise, you will have to be more explicit about what you mean by "LOV query".

    If I'm right, there are two possibilities:

    (1) you use an instance of VO in a module shared application instance. If so, make sure that your code is in the prepareSession() for the class of application that module if it is dependent on session (as seems to be below), you must make sure that the module of the application instance is shared in the session scope.
    (2) you have based the view accessor directly on the definition of the VO. This creates an anonymous instance of VO; I don't think there is a way to use prepareSession() to set a variable of liaison on such a forum (which will not be created until the accessor is first used). Pourriez be able to shoot with to put a similar code in the view object create() method class, but I've not tested this.

    If your code is in your module class application primary (as opposed to the class for a shared of AOS instance), the problem here is that getCmProcessView() returns the instance of VO data module of this application instance design-time model, which is never used by accessors of the view.

    Also, why do you need to do this in prepareSession() rather than at the level of the accessor to view? I believe you can get the user name (for the view accessor) with the groovy expression

    viewObject.DBTransaction.session.userPrincipalName

    or maybe just

    DBTransaction.session.userPrincipalName

    (despite this DBTransaction involving, it returns the username web app, not the DB user name).

    It is a declarative 100% solution, if you don't count the Groovy expression as not declarative.

    If you are afraid of a user who is running the LOV (somehow) before the value of the bind variable is preparing, just make sure that the binding variable is marked "required."

  • Ensure this soft analysis the use of Bind variables

    Hi Experts,

    I have a request when the query is prepared dynamically in Java code and if I take the query and run it from SQL Navigator, it takes a long time to prepare the statement and then execute it quickly. I think we can use dynamic SQL statements and bind variable to improve performance here. However, I tried to do a PDS to check my understanding. I created 2 procedures

    1. mode of operation
    CREATE OR REPLACE PROCEDURE sp_static
    (id IN NUMBER, 
     name in VARCHAR2)
    IS 
    TYPE r_curs_type is REF CURSOR;
    C1 r_curs_type;
    BEGIN
    
    OPEN C1
    FOR
    SELECT * FROM TABLE WHERE columname = id
    
    CLOSE C1;
    
    END;
    2. mode of operation
    CREATE PROCEDURE sp_dyanmic
    (id IN NUMBER, 
     name in VARCHAR2)
    IS 
    TYPE r_curs_type is REF CURSOR;
    C1 r_curs_type;
    BEGIN
    
    OPEN C1 FOR 'SELECT * FROM TABLE WHERE columnanme = :U1' 
    USING ID;
    
    CLOSE C1;
    END;
    To my amazement, there is no difference in the duration of execution of these procedures when I ran the with multiple entries.

    Please note-
    1. I have not access to trace files :-(
    2. I want to just make sure there will be improved performance before starting the real work, given that some efforts provided by moving the logic of all its activities inside and the use of bind variables.

    If you can suggest a strategy to ensure performance gains, it will be extremely useful...

    Thanks in advance!
    Concerning

    Hello

    (1) it's true, procedure 1 also uses a variable binding
    (2) you seem to be confused about the very basic concepts here

    Dynamic SQL is something like this:

    create or replace FUNCTION f (l_table_name VARCHAR2) RETURN NUMBER
    IS
      l_result NUMBER;
    BEGIN
      EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || l_table_name  INTO l_result;
      return l_result;
    END;
    

    In this example, since you do not know the name of the table to the execution table, everything you can
    is dynamic SQL. As you can see, neither of your two procedures is like that.

    Dynamic SQL is used when you do not know what columns you want to select, or who
    etc. to the execution table. You can also use dynamic SQL statements to force analysis. You can not use
    to avoid parsing, binding or not binding.

    Best regards
    Nikolai

  • Error (19,19): PLS-00049: bad bind variable 'P_WHERE '.

    Hello world

    Making my first attempt to bind variables. What I've read it really improves performance.

    The code below works fine until I try to do TEST1_PROC. P_WHERE a bind variable with the ':' prefixed, such as: P_WHERE.

    Suggestions?

    Thanks in advance for your help,

    Lou

    create or replace
    PROCEDURE TEST2_PROC
    AS
    my_refcur SYS_REFCURSOR;
    my_id ci_summrpt_report_codes.id% TYPE;
    my_descr ci_summrpt_report_codes.descr% TYPE;
    my_where VARCHAR2 (10): = '1.4'.
    BEGIN
    TEST1_PROC (my_refcur, my_where);
    DBMS_OUTPUT. PUT_LINE ('ID DESCR');
    DBMS_OUTPUT. PUT_LINE ('-');
    LOOP
    EXTRACTION INTO my_id, my_descr my_refcur;
    OUTPUT
    WHEN my_refcur % NOTFOUND;
    DBMS_OUTPUT. Put_line(my_id ||) CHR (9) | my_descr);
    END LOOP;
    CLOSE My_refcur;
    END TEST2_PROC;


    CREATE OR REPLACE
    PROCEDURE TEST1_PROC
    (
    p_refcur ON SYS_REFCURSOR,
    p_where IN VARCHAR2)
    IS
    v_id NUMBER (2);
    v_descr VARCHAR2 (25);
    v_select VARCHAR2 (200);
    BEGIN
    v_select: ='SELECT * FROM
    (SELECT 1 AS 'ID', 'ONE' AS 'DESCR' OF THE DOUBLE
    UNION ALL
    SELECT 2, 'TWO' OF THE DOUBLE
    UNION ALL
    SELECT 3, "THREE" DOUBLE
    UNION ALL
    SELECT OPTION 4, "FOUR" DOUBLE)
    WHERE ID IN (' |: p_where |) ')';
    DBMS_OUTPUT. Put_line (v_select);
    P_refcur OPEN FOR v_select;
    END TEST1_PROC;

    You do not specify bind variables by a colon ': ' prefix in PL/SQL.

    The stored procedure generates a query using the string concatenation. If you remove your name from the variable of the colon you still don't use bind variables.

    If you want to use bind variables you should consider the following:

    1. use the DBMS_SQL package.
    2. use EXECUTE IMMEDIATE with the USING clause.
    3. use OPEN TO with the USING clause.

    Each has different advantages/disadvantages.

    If looks as if you want to pass in a dynamic list. If you are using bind variables, it will effectively address the entire list as a value surrounded by single quotes. If you really want a dynamic IN the list, you need to investigate another method.

    Tom Kyte contains information about the lists into dynamic here: [how to make a variable in the list? | http://asktom.oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:210612357425]

  • 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

  • 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

Maybe you are looking for

  • How to allow songs bought with diff apple ID

    I bought songs as initially in 1997 with an another apple id, pw and e-mail address - it seems that the apple ID that appears is no longer valid, and although I tried several other email addresses, none work (nor do I have access to them.

  • HP Desket 1510: HP Deskjet 1510

    I have the original installation disc and it will not install the necessary software to scan or copy the document and there is no installed desktop icon.  I have also downloaded the software and drivers from HP, as well as software troubleshooting an

  • Satellite A200 - 1JS PSAE6: only stereo sound using HDMI - why?

    Hello I have an A200-1JS (PSAE6) with a HDMI and HD DVD player.I connect my A200 to a LCD screen with an HDMI cable and its ok. Good picture and sound. BUT, when I want to see a movie (without using the HD DVD player) with a surround sound (5.1) the

  • Satellite P100-188: how to perform the procedure for recovery of the product?

    Hello I just had a bit of trouble to install a piece of software that, in the end I could settle with a system restore. This process was worried at the time that I could not get the software uninstalled properly (I had to download a program from the

  • Is there a compatibility issue with model Sony KDL40V3000

    I just buy Apple TV and cannot install it with the above model. Any help would be welcome. It is a production of 2007 and I wonder if it is too old.