473,545 Members | 2,005 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 9337
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
5884
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
2237
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'...
6
5708
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...
1
6119
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
3447
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
2768
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...
10
2408
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,...
2
2485
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
3872
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
7479
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...
0
7411
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7439
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...
1
5343
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...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1901
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
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.