473,623 Members | 2,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9341
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
5888
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 Bytes=7063896) How can I correlate which part of the SQL statement is running on full table scan. Please see below for the code and explain plan SQL Code
13
2243
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 select the table which can be big. How do I do it using javascript? I tried: <INPUT TYPE=Button NAME='Select' SIZE='10' VALUE='SelctTable' onClick="document.MyTable.select();">
6
5715
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) wasn't a Table Scan. thought this might be a MainFrame thing; but a look at DB2Info on my UDB 7.2 server showed the same thing. this is truly puzzling. is there an explanation of EXPLAIN which describes how to get a Table Scan???? or are we...
1
6121
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
3458
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 this date column produces a table scan, even a simple select DATECOL from MYTABLE where DATECOL=date('2004-09-16')
3
2774
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 running explain for another database (same schema structure), it showed table scan. These are all production databases with >10 GB of real data, and stats are all up to date. Reorgchk showed nothing bad. Per chance visual explain GUI was acting...
10
2418
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 version of DB2 one or the other answer of a multiple choice question could be right, the candidate said. The HRM asked me to look into the matter, but since I am no expert in the workings of DB2 I submit the question to you hoping any of you can
2
2492
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 on various queries.
4
3877
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 variables declared in SP. select * from table where pred1 and pred2 and pred3 and variable1 ='number1 and variable2 =number2;
0
8224
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8667
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8469
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7145
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6104
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.