one question on histograms
A reader, July 02, 2003 - 8:57 pm UTC
I understand your lucid explanation for the case
against using bind variables. Could you please explain
what you meant by the following statement?
"Histograms can be used but their value is limited with bind variables."
Thanx!
July 03, 2003 - 8:54 am UTC
when using bind variables:
select * from t where x = :y;
the optimizer will use bind variable "peeking" in 9i the first time the query is hard parsed (it'll peek at the binds and optimize the query as if the literal value was there). The plan you get depends on the bind variable first used -- the histograms won't be used afterwards. they are of "limited use".
in 8i, it'll use "heuristics" to make assumptions about how much data would be returned. the histograms are of very limited use here -- since the optimizer makes no assumption as to the value of :y
if you use
select * from t where x = 1;
....where x = 2;
and so on...
then histograms come into play all of the time.
On the Topic of Bind Variables
Brian, July 03, 2003 - 11:52 am UTC
On the topic of when to use them:
I have read through your book, and I understand that I should use bind variables. But I'm not clear about two things:
1) What the syntax is exactly? I realize that in sql*plus
you can type in variable x number, and it's a bind
variable. Then, the x shows up as a :x. But what if
you're writing a procedure? How do you bind the
parameters that will later be used in a where
statement? Or are they already bound? And that leads
me to:
2) When do bind variables already exist implicitly (without
declaration or special syntax)?
I find the Oracle documentation around bind variables to not be very clear on the above.
Thanks
July 03, 2003 - 7:59 pm UTC
1) that is the pure and sheer beauty of plsql -- all variables in SQL in PLSQL are in fact binds:
create procedure foo( p_input in varchar2 )
is
begin
for x in ( select * from t where column = p_input )
loop
....
p_input *is* a bind...
2) correct -- PLSQL is so tightly coupled with SQL that the distinction blurs.
How do I make this work
sonali, January 20, 2004 - 3:22 pm UTC
As I want to see the values returned by the select statement I am doing into but as because there are multiple rows returned by this query I get this error.. How do i write this query then ?
Server Output ON
SQLWKS> DECLARE
2> Category NUMBER := 1669;
3> Legal_type NUMBER := 3;
4> Legal_locale varchar2(10):= 'en-us';
5> p_Legal_text varchar2 (25);
6> BEGIN
7>
8>
9>
10> EXECUTE IMMEDIATE 'select legal_text from mweblegal where legal_category = :bind_category
11> And legal_type = :bind_type and legal_locale = :bind_locale' into p_Legal_text
12> using category, legal_type, legal_locale ;
13> dbms_output.put_line('legal_text '|| p_Legal_text);
14>
15> END;
16>
17>
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at line 10
January 20, 2004 - 3:45 pm UTC
declare
....
type rc is ref cursor;
l_cursor is rc;
begin
open l_cursor for 'select ....' using bind1, bind2;
loop
fetch l_cursor into .......;
exit when l_cursor%notfound;
....
end loop;
close l_cursor;
end;
/
which are the advantages of dind variables in a DWH environnement?
Maurice, April 23, 2004 - 8:46 am UTC
Hi Tom,
In a DWH environnement the probability that a same query is exectued twice within a short amount of time is really really small. Also most DWH are not perfectly optimized and it often happen that a query takes from several minutes to several hours. So even if the optimzer has to reparse twice a same statment, the lost time, related to the total time of the query, is insignificant. So I just don't see, in a DWH environnement, any advantages of using bind variables.
However I noticed 2 disadvantages (on 9iR2):
- with bind variable the CBO dosent make a as precise estimation as when using litterales
- the optimizer will not do a query rewrite if a MV contains litterals which are replaced by bind variables in the query.
Conclusion:
In a DWH environnement I see no advantages of using bind variables but I noticed 2 disadvantages. So why should I use bind variables?
Regards
Maurice
April 23, 2004 - 1:30 pm UTC
that is exactly what I've written before -- in fact, in a DW, certain optimizations (star transformation for example) are not available if you bind.
so, ad-hoc queries in DW's -- don't necessarily bind them.
Binds w/ CONTAINS
Andy, December 16, 2004 - 11:07 am UTC
Hi Tom,
Assuming that my data warehouse doesn't fall into the category you described in your original answer (i.e. I DO want to use binds with my CONTAINS queries), how can I do that in such a way as not to "hide" what operators have been used?
For example:
mires@WS2TEST> var x varchar2(20)
mires@WS2TEST>
mires@WS2TEST> select count(*) from fulltext where contains(akteinh, 'NEAR((test, entry))', 1) > 0
COUNT(*)
----------
2
mires@WS2TEST> begin :x := 'NEAR((test, entry))'; end;
2 /
PL/SQL-Prozedur wurde erfolgreich abgeschlossen.
mires@WS2TEST> select count(*) from fulltext where contains(akteinh, :x, 1) > 0;
COUNT(*)
----------
2
More than just the variable is bound - the NEAR operator is as well. Is there any way to bind just the variable, and not other information that I may want in my trace file?
December 16, 2004 - 11:45 am UTC
no, because the query to text is itself just a string.
suppose you could this tho:
ops$tkyte@ORA9IR2> create table t ( x varchar2(50) );
Table created.
ops$tkyte@ORA9IR2> insert into t values ( 'how now brown cow' );
1 row created.
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> create index t_idx on t(x) indextype is ctxsys.context;
Index created.
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> variable x1 varchar2(20)
ops$tkyte@ORA9IR2> variable x2 varchar2(20)
ops$tkyte@ORA9IR2> exec :x1 := 'how'; :x2 := 'now'
PL/SQL procedure successfully completed.
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> select *
2 from t
3 where contains( x, 'near((' || :x1 || ',' || :x2 || '))', 1 ) > 0;
X
--------------------------------------------------
how now brown cow