473,405 Members | 2,279 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,405 software developers and data experts.

Does 'select 1 from mytable where 1 = 0' do a table scan?


Hi. Some DBMSes are clever enough not to go to data pages if a
knowably constant search criterion is false. Is DB2 among them?

thanks,
Joe Weinstein at BEA

Nov 12 '05 #1
7 9330
On Mon, 15 Nov 2004 08:50:46 -0800, Joe Weinstein wrote:
Hi. Some DBMSes are clever enough not to go to data pages if a
knowably constant search criterion is false. Is DB2 among them?


DB2 is smart enough to see that (1=0) and (4>5) will never be true, and
will do neither a table scan nor an index scan for such a simple query.

I don't know the limits of smartness, though (i.e. when predicated become
too tricky to decide on).

--
Greetings from Troels Arvin, Copenhagen, Denmark

Nov 12 '05 #2
The clever approach to answering this question is to do an "explain" on
the query, using a simple table (ie. the sample tables that come with
UDB) and examining the results.

If your predicate stated "where 1 = 1" then an output row would be
generated for every row in the table. A scan would be needed to generate
the correct number of output rows. If the developers of a retrieval
engine are foolish enough to look for this type of code and write
special logic to save the user from his/her own bad coding, then they
deserve the problems it can easily cause. Handling predicates in a
consistant manner, without special case code, is the way to long term
stability, consistancy, and overall performance in the retrieval engine.

Philip Sherman
Joe Weinstein wrote:

Hi. Some DBMSes are clever enough not to go to data pages if a
knowably constant search criterion is false. Is DB2 among them?

thanks,
Joe Weinstein at BEA


Nov 12 '05 #3


Troels Arvin wrote:
On Mon, 15 Nov 2004 08:50:46 -0800, Joe Weinstein wrote:
Hi. Some DBMSes are clever enough not to go to data pages if a
knowably constant search criterion is false. Is DB2 among them?


DB2 is smart enough to see that (1=0) and (4>5) will never be true, and
will do neither a table scan nor an index scan for such a simple query.

I don't know the limits of smartness, though (i.e. when predicated become
too tricky to decide on).


Thanks!

Nov 12 '05 #4
Philip Sherman wrote:
The clever approach to answering this question is to do an "explain" on
the query, using a simple table (ie. the sample tables that come with
UDB) and examining the results.
Thanks!
As long as the assumption that a DB2 instance is available to the asker is valid.
If your predicate stated "where 1 = 1" then an output row would be
generated for every row in the table. A scan would be needed to generate
the correct number of output rows.
That would be assumed. I'm just interested in the obvious 1 = 0 case.
I hope my assumption, that you are circumlocuting the answer,
"DB2 will not go to an index or to data for such a query" is correct.

If the developers of a retrieval engine are foolish enough to look for this type of code and write
special logic to save the user from his/her own bad coding, then they
deserve the problems it can easily cause. Handling predicates in a
consistant manner, without special case code, is the way to long term
stability, consistancy, and overall performance in the retrieval engine.
Thanks again. I'm not sure who you're referring to, but I've seen some
applications do that (adding a where 1 = 0 clause) even onto queries they
didn't generate themselves, in order to get the metadata about the query,
which will be sent to the client, even for a zero-row return. Not me, but
I get paid to deal with other's problems, even self-inflicted...
Philip Sherman
Joe Weinstein wrote:

Hi. Some DBMSes are clever enough not to go to data pages if a
knowably constant search criterion is false. Is DB2 among them?

thanks,
Joe Weinstein at BEA


Nov 12 '05 #5
Joe Weinstein wrote:

Hi. Some DBMSes are clever enough not to go to data pages if a
knowably constant search criterion is false. Is DB2 among them?

thanks,
Joe Weinstein at BEA


Yes.
Nov 12 '05 #6
On Mon, 15 Nov 2004 15:58:53 -0500, Serge Rielau wrote:
Hi. Some DBMSes are clever enough not to go to data pages if a
knowably constant search criterion is false. Is DB2 among them?
[...] Yes.


Is there any good documentation on what kinds of semantic query
optimizations like this DB2 tries to perform?

--
Greetings from Troels Arvin, Copenhagen, Denmark

Nov 12 '05 #7
Troels Arvin wrote:
On Mon, 15 Nov 2004 15:58:53 -0500, Serge Rielau wrote:

Hi. Some DBMSes are clever enough not to go to data pages if a
knowably constant search criterion is false. Is DB2 among them?


[...]
Yes.

Is there any good documentation on what kinds of semantic query
optimizations like this DB2 tries to perform?

Not that I'm aware of. DB2 UDB for LUW knows some 100 semantic query
rewrite rules. Some of which have patented algorithms which you can look
up, but in general this is all part of the secret mix ;-)
Here is a link to the "theorem prover" (note that it's not fully
exploited in rewrite to keep the compiler snappy :) It was originally
added in DB2 V5.2 for typed view hierarchy optimization.
http://patft.uspto.gov/netacgi/nph-P...&RS=PN/6728952

Cheers
Serge
Nov 12 '05 #8

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

Similar topics

3
by: Marcus | last post by:
Hi I have a very complex sql query and a explain plan. I found there is a full table scan in ID=9 9 8 TABLE ACCESS (FULL) OF 'F_LOTTXNHIST' (Cost=84573 Card=185892...
13
by: Botao | last post by:
Hi, Every Guru, I'd like to put a button on a page. When clicking the button, the table below it gets selected so the user can do Ctrl C to copy the entire table without using the mouse to...
6
by: robert | last post by:
just got out of a "class" on EXPLAIN, from a 390/v6 maven. was told that ACCESSTYPE = 'R' in the plan_table meant a TableSpace scan. hmmmm. next step up (or down, depending on your outlook)...
1
by: Mike L. Bell | last post by:
Query: update table1 t1 set end_time = ( select end_time from table2 t2 where t2.key1 = t1.key1 and t2.key2 = t1.key2 ) where exists
5
by: news.swissonline.ch | last post by:
Hi, DB2 7 FP 13 Windows XP I have a table in which on of the columns has the data type DATE. The date column is indexed (asc non-unique index). Every SQL I create which tries to filter by...
3
by: selma | last post by:
I am puzzled by visual explain for a simple query ( select * from mytable ) chooses an index scan. That index has only a single column and the table has 30 columns not covered by the index. When...
10
by: Dia | last post by:
At the company I work job applicants are required to do a little test. The human resource manager recently had a candidate who claimed one of the questions was ambiguous. Dependent upon the...
2
by: BD | last post by:
Hi, all. My background is more Oracle than db2. My skills at SQL tuning are quite limited. I'm running 8.2 on Windows. I'm tasked with some SQL optimization, and am doing some explain plans...
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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.