If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Query uses index 98% of the time

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-20-09, 16:20
Sheerah Sheerah is offline
Registered User
 
Join Date: Jan 2009
Location: Sacramento, CA
Posts: 5
Question Query uses index 98% of the time

Hi experts! Riddle me this.....

My system is very, very small. It only has 17 tables and about 20 users.

I have a query that gets executed many times throughout the day. 98% of the time the query uses an index that I created to specifically make the query more efficient. The other 2% of the time the query uses a tablescan. The SQL is always executed dynamically.

My developer recently added FOR FETCH ONLY WITH UR to the query.

My system is such that it would be quite possible for one user to be updating a particular row while a different user is viewing that same row.

My suspicion is that because we are doing a dirty read with this query, if another user happens to be updating that row at the same time, the optimizer says "Hey, I need to update the index as well, so I'm not going to read the table using the index."

I'm new at this DBA stuff. Do you think that my suspicion is correct?

Last edited by Sheerah; 10-20-09 at 16:42.
Reply With Quote
  #2 (permalink)  
Old 10-20-09, 16:47
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
I would doubt that. If the query has literals in the predicate (WHERE clause) DB2 can check the value against the statistics to estimate how many of that value exist in the database and adjust the access plan accordingly. If you don't want DB2 to do that, then get rid of the distribution stats for the columns for the table that are stored in the catalog (and don't ask for distribution stats when you do runstats).

However, there is good chance that when DB2 is doing a table scan, that is the most efficient access path for that query (assuming you have accurate stats).
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 10-20-09, 17:04
Sheerah Sheerah is offline
Registered User
 
Join Date: Jan 2009
Location: Sacramento, CA
Posts: 5
Hi Marcus. I do have literals in the WHERE clause and my stats should be accurate.

I don't know what "distribution stats" are. My RUNSTATS looks like this:

RUNSTATS TABLESPACE LIST PRDLIST3
TABLE (ALL)
INDEX (ALL)
SHRLEVEL CHANGE
REPORT YES
UPDATE ALL

Anyhow, off to read about distribution statistics now.
Reply With Quote
  #4 (permalink)  
Old 10-20-09, 17:21
Sheerah Sheerah is offline
Registered User
 
Join Date: Jan 2009
Location: Sacramento, CA
Posts: 5
Quote:
Originally Posted by Marcus_A
I would doubt that. If the query has literals in the predicate (WHERE clause) DB2 can check the value against the statistics to estimate how many of that value exist in the database and adjust the access plan accordingly. If you don't want DB2 to do that, then get rid of the distribution stats for the columns for the table that are stored in the catalog (and don't ask for distribution stats when you do runstats).

However, there is good chance that when DB2 is doing a table scan, that is the most efficient access path for that query (assuming you have accurate stats).
After reading about distribution statistics, it seems to me that it is best to collect them. (I'm still trying to figure out if we're collecting them or not.) However, I don't understand why, when executing the exact same query, just a few hours apart, one execution would use the index and one execution would not.
Reply With Quote
  #5 (permalink)  
Old 10-20-09, 17:54
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
I didn't realize until now that you are using DB2 z/OS. Also, I didn't realize it was the exact same query (with exact same literals) each time. So I am not sure about why that is happening.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #6 (permalink)  
Old 10-21-09, 09:05
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
is this a dynamic query or something that is bound to the DB? How are you determining that it is not using the index, are you performing an explain or looking at some monitor?
Dave
Reply With Quote
  #7 (permalink)  
Old 10-22-09, 13:45
Sheerah Sheerah is offline
Registered User
 
Join Date: Jan 2009
Location: Sacramento, CA
Posts: 5
Quote:
Originally Posted by dav1mo
is this a dynamic query or something that is bound to the DB? How are you determining that it is not using the index, are you performing an explain or looking at some monitor?
Dave
This is a dynamic query, Dave. I am running an explain that tells me if the query uses any indexes or if it does a tablescan.
Reply With Quote
  #8 (permalink)  
Old 10-22-09, 15:22
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
You don't mention which version of DB2 you are running or platform, but based off the runstats that you showed above, we determined you are on Z/OS. I would suggest you try collecting freqval values for your indexes. Also, You may want to ask your question on the DB2-L listserv on idug.org as you may get an accurate explanation from Pat Bossman or Terry Purcell over there.
I can't think of any reason the exact same query with the exact same literals being supplied would give you different access paths, unless runstats had been run in between the times you explained the query. If, however, it were the same query with different literals being used it could be explained that you have uneven distribution of values in that column(skewed data). For instance, 98 - 99% of the time a flag has a value of N and the remaining rows have a value of Y. The index could be chosen for the rows with a value of Y, but the optimizer would opt for a tablespace scan if the SQL was looking for the value N.
Dave Nance
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On