Bind variable: always related to the current attribute of the iterator line
Hi, OTN,.Need some help with the variable binding query of the view object.
I have a VO1 with: PARAM in a subquery in the WHERE clause.
This: PARAM must always be equal to the current attribute of the iterator of VO2 line.
VO1 and VO2 are not related, but their of1 and EO2 entities has an association.
VO2 is exposed as an array on my page, so I did it with the table selection listener.
He calls makeCurrent parameter named where clause param for VO1 and VO2.
This method works, but the problem is selection listener is called only when a table row is explicitly selected.
The current row of the iterator can be changed whithout table selection, such as sorting or the parent navigation of VO...
Is there a world better, maybe a declarative method for this problem?
Thank you.
Example query VO1:
SELECT ... FROM EO1
WHERE ID != (SELECT ID FROM ... WHERE attr = :PARAM)
JDeveloper 11.1.1.3, AD, BC
I guess you try relationship master / detail (Table-based form)... you must see link
Refer to this:
http://download.Oracle.com/docs/CD/E14571_01/Web.1111/b31974/web_masterdetail.htm#ADFFD761
Chapter 24 - master data display / detail
Amit
Tags: Java
Similar Questions
-
Hello
Can someone help me with the problem I am facing to... my scenario is currently that we show reports from 00 hours up to 24 hours. But now the client wants to exact more than 24 hours for chart reports.
This means that if the time is now 16:05 hours, then graphical report must display data for 16:00 Hrs from yesterday until 16:00 hours that day. Is the same for the last week day 24 hours as well. Means they need two lines in the chart. Please help me...
Thank you
Srini VIEREN Terry.Williams dileraco
Hello
Now using these variables filter your column of measure...
Kind regards
Naga
-
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:
HelloHere 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...
-
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.
@>
-
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
-
Ask about bind variables and LOVs
Hello
I'm new to ADF. Last day, I learned about LOVS, see criteria and bind variables. Now, I tried to develop a sample application. I created a few EOs, village and an AM.
I also defined a view criteria which takes as a parameter a variable binding and rule the condition when running. Then, I created a group of query based on my view criteria. In this query Panel, when I enter the name of a student in a box (which is related to the binding variable that I guess), the student information is displayed in a table. Now, instead of an entry box, I want a list of choices based on a LOV-driven model that will contain all the names of students available. Is this possible in ADF 11 g?
Thanks in advanceYou must open the VO, you use, go to the view whole section and in the view whole section go in the attributes section.
In attributes, select the attribute by which you are to the search or make your operation, click on the button add to the list of values: the name of the attribute
Select the vo object to fill the value.
You will get the data in the variable binding, that you created in the view criteria.
Reporter, you can follow the bolg
http://saumoinak.blogspot.com/2011/02/showing-data-based-on-list-of-values.html
-
Interactive report Bind variable not working do not?
Hello
I'm new to APEX and try to find how to use APEX of interactive reports with bind variables. So, I created one with the following query:
SELECT i1.if_bic_code, b.bicname, count (*) NTC
OF IF_DATA i1, bic b
WHERE i1.if_bic_code = b.biccode (+)
AND i1.insert_date BETWEEN: P40_X1 AND: P40_X2
I1.if_bic_code GROUP, b.bicname
The report is supposed to show a number of comments by the bic_code (= bicname) of the IF_DATA table for the specified period.
These 2 bind variables are actually 2 Articles in the same region, with the help of a DatePicker for them. They have no initial values. Thus, the report displays initially no record. After either by using the date picker or manually enter values in these 2 points and pressing go button doesn't always display no line. Am I missing something here? It seems that the query again to the database, or the bind variables do not take the values entered for them.
TIAHello
If you are the Go button that appears on the interactive report action bar, you must add in page elements names that must be saved when you do this, as the page itself is not subject to (which is the normal action that saves your dates).
To do this, change the attributes of the report and go to the bottom of the page. You should see a section attributes advanced with a parameter "Page to go". In this, enter:
P40_X1,P40_X2
-C' is a list separated by commas to names of page elements that you saved when you click the Go button
Andy
-
Hello
If I run this query from my sql * more customer, what is the difference between substitution and the bind variable then?
Select * from emp where empno = & x;
Select * from emp where empno =: x;
In both cases, my request that will be sent to the server for execution will be completed (with no reserved space).
The only difference I can think is that we cannot use bind variable instead of table_names & the names of column, but we can use substitution variables. As shown below
Select * from: var where empno =:-this will not work when they are under values dynamically
Select * from & var where empno = & y - it works.
So what else is the difference between substitution and the bind variable then?
Thank you
When you use a variable substitiution in sqlplus or any other client that supports, the client interprets the value of the variable and puts it into the sql statement. So, if you have:
Select * from emp where emp_id = & n
The customer gets the value you supply and replaces the variable substitution with the value you provide, thus, if you enter 10 the query that the sql engine sees is actually:
Select * from emp where emp_id = 10
This is why you must put quotes around a variable substitution when using it as a string in a projection or a predicate. In other words, if you want to query by last_name with a substitution variable, you would do something like:
Select * from emp where last_name = '& ln '.
When you use a link as variable:
Select * from emp where emp_id =: n
the client passes the sql statement, exactly as you wrote it and at the same time he says also, moreover, the value of: n is 10. If you then change the value of: n 20 the customer always going the same exact sql statement, but this time, says the value of: n is 20.
As others have pointed out, with a substitution variable each sql statement with a new value for the variable is a brand new, never seen before trial and must be hard to be analyzed by the SQL engine. Whenever you use a new value of a variable to bind the SQL remains the same, so the SQL engine can avoid the hard analysis (expensive).
John
-
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. -
Using Bind Variable in a SELECT statement
Hello
I am trying to build my SQL query running using bind variables and in Oracle® Fusion Middleware Fusion developer Guide for Oracle Application Development Framework 11 g Release 1 (11.1.1) it is said that ' after you define bind variables, the next step is to reference them in the SQL statement. While SQL syntax allows you to bind variables to appear in the SELECT list and in the WHERE clause, you'll generally use them in this context, as part of your WHERE clause. ».
However, when I try to use bind variables in my SELECT list because I had set a type to the string of the variable variable is inserted with quotes each side for example SELECT TestTable FROM 'Service '. Is it possible to use bind variables to insert a value in my list of selection without the quotes around it?
Thanks in advance,
TomHi Robinst,
I think you want to set up column name of the table that is not possible using bind variables. With the help of the bind variables you can send a value to the SQL. The String value is therefore always with inverted commas.
Kind regards
Branislav
-
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 )
-
Hi all
I have a variable refcursor, to which I will assign running queries.
All are defined (union) queries, with 5 or 6 selects the union, each select returns the same variable where clause. Value of the variable will be known at run time.
For example: (just an example)
{code}
Start
v_sql: = ' select col1, col2
of the Department
where dept_no =: 1
Union
Select col1, col2
of eur_dept
where dept_no =: 1
Union
Select col1, col2
of us_dept
where dept_no =: 1 ';
Open the v_ref_cur for v_sql using 20, 20, 20;
-rest of the logic
------------
-----------------
end;
{code}
Question is how to avoid the bind variable value repeat (in the open statement). Is there a way I can mention value 20 only once when the cursor is opened.
Thank you!
DS says:
Question is how to avoid the bind variable value repeat (in the open statement). Is there a way I can mention value 20 only once when the cursor is opened.
Yes - can be done using a dynamic anonymous block to create the refcursor (containing the SQL text), as oppose to the creation of the refcursor directly from the SQL text.
Not an approach I consider myself - unless there is absolutely no better way to address this particular issue.
SQL > var refcursor c.
SQL >
SQL > declare
News 2 sys_refcursor;
3. start
4 immediate execution
5 ' declare
6 varchar2 (4000) var1: =: 1; -/ / simple definition var - used several times
7. start
open 8: news for
9 select * from emp where mgr = var1
10 the union all the
11. Select * from emp where empno = var1;
12 end; »
13 using 7698-past the variable once
14 in the news; -getting refcursor as answer
15
16: c: = cur;
end 17;
18.
PL/SQL procedure successfully completed.
SQL >
SQL > print c
EMPNO, ENAME, JOB HIREDATE DEPTNO COMM SAL MGR
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 1981/02/20 00:00:00 1600 300 30
7521 WARD SALESMAN 7698 1981/02/22 00:00:00 1250 500 30
7654 MARTIN SALESMAN 7698 1981/09/28 00:00:00 1250 1400 30
7844 TURNER SALESMAN 7698 1981-09-08 00:00:00 1500 0 30
7900 JAMES CLERK 7698 1981/12/03 00:00:00 950 30
7698 BLAKE MANAGER 7839 1981/05/01 00:00:00 2850 30
6 selected lines.
SQL >
-
invalid host / bind variable
Hello
I get the following problem when I am trying to pass a variable to link inside the IN clause of my SQL statement, which is also within a void query, although I don't think it will make a difference?
How can I change my SQL so that it will accept a bind variable?
EXAMPLE of the SQL problem:
SELECT manager_name
Managers
WHERE Group_id IN (SELECT Group_id
Groups
WHERE IN GroupName (: myBindVariable))
)
Thank you
SteveYou cannot use the lie for a value of list... You can use links for each individual value in such a list. But not for the entire list.
Maybe this helps:
http://asktom.Oracle.com/pls/asktom/f?p=100:11:1920953816309736:P11_QUESTION_ID:139812348065
Just to clarify more what I mean, here's an example of sqlplus with table DOUBLE:
15:46:35 SQL> variable b1 varchar2(40); 15:46:48 SQL> exec :b1 := 'X'; PL/SQL procedure successfully completed. 15:47:13 SQL> select * 15:47:15 2 from dual 15:47:17 3 where dummy in (:b1); More... D - X 1 row selected. 15:47:28 SQL> exec :b1 := '''X'',''Y''' PL/SQL procedure successfully completed. 15:49:46 SQL> select * 15:49:50 2 from dual 15:49:55 3 where dummy in (:b1); no rows selected 15:50:00 SQL> exec :b1 := 'X,Y'; PL/SQL procedure successfully completed. 15:50:15 SQL> select * 15:50:19 2 from dual 15:50:21 3 where dummy in (:b1); no rows selected 15:50:26 SQL>
Published by: Toon Koppelaars on October 12, 2009 15:50
-
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 TOwith 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]
-
On the updating of the iterator
Hello
I have a view object that uses bind variables in its application (just for the record, the VO is based query not based on entities).
For ex: select emp.code, emp.name Emplyee emp where emp.dept =: DEPT_BV
This VO is displayed in a normal af:table in the page.
I have a button on the page which, when clicked, should change the value of the DEPT_BV and update the table.
Thus, in the action of this button, I'am changing the value of DEPT_BV and call the VO executeQuery(). It all works.
The question is: am I obliged to manually call vo.executeQuery () after you have changed the value of the DEPT_BV?
If we can set up the required iterator in the definition of the page (for ex by using the refresh to re-run automatically attribute)? I read in the documentation that refreshing the iterator does not necessarily mean her rerun the query.
So is it possible to do it without manually call run query?
Kind regards
Elias.
Yes, it's because you will add a new binding variable changed manually to the query.
You must tell the framework that it's my new link variable has changed and now to search for documents on this basis.
Thank you
Maybe you are looking for
-
My screen grey remains with the progress bar stuck on this update... suggestions? Mac Pro 2012 (5.1)
-
Can not read the RTC in an application deployed on cRIO9022
Hello I built a datalogging application that stores data in a file name to the name = current date/time on USB. It works fine when connected over the network to the development computer, however, when it is deployed as a start VI, section date and ti
-
Have HP Pavilion a6110n and windows Vista editions Home premium. A reboot from recovery disc and Noah update will not work. System has also disabled the netgear router and killed a comcast modem. I think I need computer trash HP 6110.
-
OpenGL and reminders of camera
Hello I try to combine the camera API with openGL (so I can generate overlays and others on the images of the camera natively) and encounter a few problems, I hope someone here can help me to solve. I use the new NDK 2.1.0 beta for the playbook and A
-
Windows 7 error code backup 2155348000. Backup is wrong every time.
Original title: error ' 2155348000 backup Windows 7 error code backup 2155348000. Backup is wrong every time.