IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Index problem in SQL Query


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
callkris_cit

New User


Joined: 22 Apr 2006
Posts: 44

PostPosted: Wed Oct 29, 2008 10:40 pm
Reply with quote

I have written the SELECT query, based on STR_ID range.

When i running the query, with some range, the query using index.
Code:

SELECT ROW_EFF_DT
FROM TABLE2
WHERE STR_ID <=   1000          AND   STR_ID >=  10000

But if i change the range its not using the index.

Code:
SELECT ROW_EFF_DT
FROM TABLE2
WHERE STR_ID <=   1000          AND   STR_ID >=  10000

What could be the reason for this
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Thu Oct 30, 2008 12:06 am
Reply with quote

Hi,

Both the queries look same to me, did I miss something here or..
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Thu Oct 30, 2008 12:07 am
Reply with quote

Hi,

Please notice your post is been edited to add the code tags, learn to use BBcode - they make the post rather readable.

-Ad
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Thu Oct 30, 2008 12:08 am
Reply with quote

Not only do they look the same to me, but also how many records are going to have STR_ID values <= 1000 and >= 10000 at the same time?
Back to top
View user's profile Send private message
callkris_cit

New User


Joined: 22 Apr 2006
Posts: 44

PostPosted: Thu Oct 30, 2008 11:16 pm
Reply with quote

I have written the SELECT query, based on STR_ID range.

When i am doing EXPLAIN, with some range, the query using index, when
Code:

SELECT ROW_EFF_DT
FROM TABLE2
WHERE STR_ID <= 1000 AND STR_ID >= 10000

( But there is no record have this range )

But if i change the range its not using the index.

Code:
SELECT ROW_EFF_DT
FROM TABLE2
WHERE STR_ID <= 20000 AND STR_ID >= 10001

( Lot of records present in this range )

What could be the reason for this
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Oct 31, 2008 12:13 am
Reply with quote

Hello,

Is there some reason you do not use the "Code" tag as requested?

Quote:
WHERE STR_ID <= 1000 AND STR_ID >= 10000

( But there is no record have this range )
Hopefully, this is not a surprise - there can never be a row that satisfies the condition icon_confused.gif

Quote:
WHERE STR_ID <= 20000 AND STR_ID >= 10001
What happens when you reverse these?
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Fri Oct 31, 2008 7:19 am
Reply with quote

Hi,

First query will show the ROW_EFF_DT for
Code:
1000 <=STR_ID <=10000
& second query will show the ROW_EFF_DT for
Code:
10001 <=STR_ID <=20000
, both the queries are "effective" on different range of STR_ID - of course there are very chances that they will show different results.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Oct 31, 2008 7:36 am
Reply with quote

Hi Anuj,

Quote:
First query will show the ROW_EFF_DT for
I think not. . .

What value can satisfy this "WHERE STR_ID <= 1000 AND STR_ID >= 10000 "?

I can think of no number that is both less than 1 thousand and also greater than 10 thousand icon_confused.gif

My second point had nothing to do with the value(s) returned but rather how db2 would handle/process the query.
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Fri Oct 31, 2008 8:06 am
Reply with quote

oops ...please read "AND" in the query as "OR"..my post would appear meanigful.. icon_redface.gif

tough crowd..ya.. icon_smile.gif

-Ad
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri Oct 31, 2008 2:45 pm
Reply with quote

not near as tough as a pre-compiler, compiler, or OP system.
Back to top
View user's profile Send private message
callkris_cit

New User


Joined: 22 Apr 2006
Posts: 44

PostPosted: Fri Oct 31, 2008 11:29 pm
Reply with quote

WHERE STR_ID >= 10001 AND STR_ID <= 20000

also not using the Index.


WHERE STR_ID >= 1 AND STR_ID <= 10000

is using the index
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Nov 01, 2008 12:49 am
Reply with quote

Hello,

It probably has to do with the optimizer and population counting.

Suggest you work with your dba to learn how/why the query is being handled in the different situations.
Back to top
View user's profile Send private message
callkris_cit

New User


Joined: 22 Apr 2006
Posts: 44

PostPosted: Sat Nov 01, 2008 1:07 am
Reply with quote

My DBA said, it may be Partition problem..

But i don't think so. Is there any possibility?
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Nov 01, 2008 1:42 am
Reply with quote

Hello,

Quote:
My DBA said, it may be Partition problem..
This is not a term i'm familiar with - what does it mean in your environment?
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Sat Nov 01, 2008 3:40 am
Reply with quote

Hi Dick,

As tables grow in size, it may be easier to manage your data in chunks or by limited ranges. Those ranges are table-partitions.

In such a scenario, applications can continue to access data by specifying column and table names, and do not need to worry about which data partition(s) the data resides in.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Nov 01, 2008 4:21 am
Reply with quote

Hi Anuj,

Yup, i'm quite with partitioned tables.

What i'm not familiar with a/the "partition problem" icon_confused.gif
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Map Vols and Problem Dataset All Other Mainframe Topics 2
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
Search our Forums:

Back to Top