473,385 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Query Optimizer does not ignore constant condition?

I came across a very strange situation at work. There is an order of
magnitude difference in execution time for the following two queries
(10 v/s ~130 msec):

select count(*) from table_name where column_name = 'value' or 1 = 0

select count(*) from table_name where column_name = 'value'

I do not want to go into the reason why the redundent condition
exists, the example is representative of the real query where it
serves a purpose.

Any suggestions what I can do to improve the situation?

Thanks in advance.
Jul 19 '05 #1
9 4724
Hi Ed,

Didn't I say I don't want to go into why it exists? <g>

Consider that about five sub-conditions, created at different
locations in the program, are OR'd together by the client code. That
is,

whereClause = part1.getCondition() + "OR" + part2.getCondition() +
"OR" ....

While usually there is always something to fill-in in each of these
sub-conditions, in one particular (exception) case the third part does
not produce a condition.

Instead of changing the client code to check whether the condition is
non-null before adding it to the where clause, the programmer decided
to return "(1=0)", assuming that the query optimizer will ignore it.
But it doesn't.

Hope this satisfies your inquiring mind.

I have modified the strucure so that this is no longer an issue. But I
would like to know why this happens.
ed********@magicinterface.com (Ed prochak) wrote in message news:<4b**************************@posting.google. com>...
h0*@hotmail.com (hemal) wrote in message news:<16**************************@posting.google. com>...
I came across a very strange situation at work. There is an order of
magnitude difference in execution time for the following two queries
(10 v/s ~130 msec):

select count(*) from table_name where column_name = 'value' or 1 = 0

select count(*) from table_name where column_name = 'value'

I do not want to go into the reason why the redundent condition
exists, the example is representative of the real query where it
serves a purpose.

Any suggestions what I can do to improve the situation?

Thanks in advance.


I have no explaination for you, just a question:
what purpose does a redundant condition serve?

Enquiring minds want to know!

ed

Jul 19 '05 #2
Post the execution plans for both cases, and then we'll be able to see
what's going on. Important data include Oracle version,
optimizer_mode, tables analyzed recently or not, ...

Daniel
Jul 19 '05 #3
Oracle version is 9i.

I don't know how to retrieve the execution plan, optimizer_mode and
the information about when the tables were analyzed. Can you please
tell me how?

I spoke the DBA and he did say something about the optimizer used.
According to him, the rule bases optimizer works better in such cases.
"Queries involving OR conditions can be difficult for the cost-based
optimizer to resolve efficiently. Sometimes (but not always) a
histogram can help. Otherwise, FULL or USE_CONCAT and INDEX hints can
be used to select the best execution plan if OR condition is
mandatory"

But, but....I would consider this particular problem to be, er,
elementary. All these optimizer_mode and table statistics are required
for deciding that ((1=0) or x) is equivalent to (x)? I am surprised.

da*************@hotmail.com (Daniel Roy) wrote in message news:<37************************@posting.google.co m>...
Post the execution plans for both cases, and then we'll be able to see
what's going on. Important data include Oracle version,
optimizer_mode, tables analyzed recently or not, ...

Daniel

Jul 19 '05 #4
h0*@hotmail.com (hemal) wrote in message news:<16**************************@posting.google. com>...
Oracle version is 9i.

I don't know how to retrieve the execution plan, optimizer_mode and
the information about when the tables were analyzed. Can you please
tell me how?

I spoke the DBA and he did say something about the optimizer used.
According to him, the rule bases optimizer works better in such cases.
"Queries involving OR conditions can be difficult for the cost-based
optimizer to resolve efficiently. Sometimes (but not always) a
histogram can help. Otherwise, FULL or USE_CONCAT and INDEX hints can
be used to select the best execution plan if OR condition is
mandatory"

But, but....I would consider this particular problem to be, er,
elementary. All these optimizer_mode and table statistics are required
for deciding that ((1=0) or x) is equivalent to (x)? I am surprised.


Well, it may SEEM elementary, but how would you code the optimizer to
recognize it? Remember that extra clause might be nearly anything:
OR -1=0
OR 2+2=3
OR -1=1
OR 0=1/0
OR 'JULY 1 1970' = '07-01-70'

In theory
<any expression evaluating to a constant>
= <any expression evaluating to another constant>

But back to your practical problem. It is a long shot, but try putting
the constant expression at the other end of the where clause. IOW if
now you use:

select * from tables
where a = b
and 0=1;

change it to be

select * from tables
where 0=1
and a = b;

I really don't know if that will help, but the optimizer does seem to
check expressions last to first. If it gets a chance to check the real
expressions first, MAYBE it will help.

Caveat THIS IS ONLY A GUESS, LONGSHOT, PIE-IN-THE-SKY, crack-pot idea.

Yes you may call me a crackpot if I'm wrong.
And I fully expect to be wrong. 8^)
Jul 19 '05 #5
Why can't the DBA help you on getting this info?
Oracle version is 9i.

I don't know how to retrieve the execution plan, optimizer_mode and
the information about when the tables were analyzed. Can you please
tell me how?

I spoke the DBA and he did say something about the optimizer used.
According to him, the rule bases optimizer works better in such cases.
"Queries involving OR conditions can be difficult for the cost-based
optimizer to resolve efficiently. Sometimes (but not always) a
histogram can help. Otherwise, FULL or USE_CONCAT and INDEX hints can
be used to select the best execution plan if OR condition is
mandatory"

But, but....I would consider this particular problem to be, er,
elementary. All these optimizer_mode and table statistics are required
for deciding that ((1=0) or x) is equivalent to (x)? I am surprised.

da*************@hotmail.com (Daniel Roy) wrote in message news:<37************************@posting.google.co m>...
Post the execution plans for both cases, and then we'll be able to see
what's going on. Important data include Oracle version,
optimizer_mode, tables analyzed recently or not, ...

Daniel

Jul 19 '05 #6
Post the execution plans for both cases, and then we'll be able to see
what's going on. Important data include Oracle version,
optimizer_mode, tables analyzed recently or not, ...

Daniel
Jun 27 '08 #7
Oracle version is 9i.

I don't know how to retrieve the execution plan, optimizer_mode and
the information about when the tables were analyzed. Can you please
tell me how?

I spoke the DBA and he did say something about the optimizer used.
According to him, the rule bases optimizer works better in such cases.
"Queries involving OR conditions can be difficult for the cost-based
optimizer to resolve efficiently. Sometimes (but not always) a
histogram can help. Otherwise, FULL or USE_CONCAT and INDEX hints can
be used to select the best execution plan if OR condition is
mandatory"

But, but....I would consider this particular problem to be, er,
elementary. All these optimizer_mode and table statistics are required
for deciding that ((1=0) or x) is equivalent to (x)? I am surprised.

da*************@hotmail.com (Daniel Roy) wrote in message news:<37************************@posting.google.co m>...
Post the execution plans for both cases, and then we'll be able to see
what's going on. Important data include Oracle version,
optimizer_mode, tables analyzed recently or not, ...

Daniel
Jun 27 '08 #8
h0*@hotmail.com (hemal) wrote in message news:<16**************************@posting.google. com>...
Oracle version is 9i.

I don't know how to retrieve the execution plan, optimizer_mode and
the information about when the tables were analyzed. Can you please
tell me how?

I spoke the DBA and he did say something about the optimizer used.
According to him, the rule bases optimizer works better in such cases.
"Queries involving OR conditions can be difficult for the cost-based
optimizer to resolve efficiently. Sometimes (but not always) a
histogram can help. Otherwise, FULL or USE_CONCAT and INDEX hints can
be used to select the best execution plan if OR condition is
mandatory"

But, but....I would consider this particular problem to be, er,
elementary. All these optimizer_mode and table statistics are required
for deciding that ((1=0) or x) is equivalent to (x)? I am surprised.
Well, it may SEEM elementary, but how would you code the optimizer to
recognize it? Remember that extra clause might be nearly anything:
OR -1=0
OR 2+2=3
OR -1=1
OR 0=1/0
OR 'JULY 1 1970' = '07-01-70'

In theory
<any expression evaluating to a constant>
= <any expression evaluating to another constant>

But back to your practical problem. It is a long shot, but try putting
the constant expression at the other end of the where clause. IOW if
now you use:

select * from tables
where a = b
and 0=1;

change it to be

select * from tables
where 0=1
and a = b;

I really don't know if that will help, but the optimizer does seem to
check expressions last to first. If it gets a chance to check the real
expressions first, MAYBE it will help.

Caveat THIS IS ONLY A GUESS, LONGSHOT, PIE-IN-THE-SKY, crack-pot idea.

Yes you may call me a crackpot if I'm wrong.
And I fully expect to be wrong. 8^)
Jun 27 '08 #9
Why can't the DBA help you on getting this info?
Oracle version is 9i.

I don't know how to retrieve the execution plan, optimizer_mode and
the information about when the tables were analyzed. Can you please
tell me how?

I spoke the DBA and he did say something about the optimizer used.
According to him, the rule bases optimizer works better in such cases.
"Queries involving OR conditions can be difficult for the cost-based
optimizer to resolve efficiently. Sometimes (but not always) a
histogram can help. Otherwise, FULL or USE_CONCAT and INDEX hints can
be used to select the best execution plan if OR condition is
mandatory"

But, but....I would consider this particular problem to be, er,
elementary. All these optimizer_mode and table statistics are required
for deciding that ((1=0) or x) is equivalent to (x)? I am surprised.

da*************@hotmail.com (Daniel Roy) wrote in message news:<37************************@posting.google.co m>...
Post the execution plans for both cases, and then we'll be able to see
what's going on. Important data include Oracle version,
optimizer_mode, tables analyzed recently or not, ...

Daniel
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Eugenio | last post by:
Excuse me in advance fo my little English. I've got this stored procedure **************************************************************************** ********** declare @Azienda as...
22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
7
by: Ion | last post by:
Hi all, I have a query that takes almost 1 hour to complete. This is acceptable in certain situations, but unacceptable when no rows should qualify. Something like: Select list >From...
15
by: Jean | last post by:
Hello, I have the following query that I set up as a test, and it runs fine: SELECT STATUSHISTORIE.* FROM STATUSHISTORIE LEFT JOIN PROBLEM_DE ON STATUSHISTORIE.PROBLEM_ID =...
4
by: uspensky | last post by:
I have a table (cars) with 3 fields: VIN, Class, sell_price 101, sports, 10000 102, sports, 11000 103, luxury, 9000 104, sports, 11000 105, sports, 11000 106, luxury, 5000 107, sports, 11000
2
by: Eckhart | last post by:
Dear All, Plz help me in optimising the following query, Reduce repeatable reads from the table via select ,ythe table sare not having referntial integrity constarints ,relations CREATE proc...
10
by: Raj | last post by:
I have an MDC index on Big_A.Dt column. The following query always goes for a table scan. SELECT Key, Cd, Dt, SUM(Big_A ) FROM ( SELECT Big_A.Key , small_3.Cd,
29
by: wizofaus | last post by:
I previously posted about a problem where it seemed that changing the case of the word "BY" in a SELECT query was causing it to run much much faster. Now I've hit the same thing again, where...
4
by: Arun Srinivasan | last post by:
Hi I was using a query previously, that was efficient select * from table where pred1 and pred2 and pred3; Later I was asked to introduce new ones, but they were not based on table columns but...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.