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 > with or without Optimize for clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-07-11, 13:00
rangupt rangupt is offline
Registered User
 
Join Date: Jan 2008
Posts: 96
with or without Optimize for clause

Hi All,

ENV: Db2 9.5 FP5, RedHat Linux 5.3

I have a statment that sometimes takes too long to or not complete at all:

"Select auth_id from db2inst1.datamodel order by auth_id desc fetch first 1 rows only optimize for 1 rows"

There is an index that exists for auth_id and the explain plan reveals that the index is being used. The table datamodel isn't very big and has only 500 some rows in it.

Would it be better for performace to NOT use the optimize for clause in this situation?

Any other pointers.

Thanks
Reply With Quote
  #2 (permalink)  
Old 02-07-11, 14:09
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
rangupt, why don't you just use:
Code:
SELECT MAX(AUTH_ID)
FROM db2inst1.datamode
If the Index allows a reverse scan (and AUTH_ID is the first or only key), this should be a 1 fetch index access (about the best you can get)
Reply With Quote
  #3 (permalink)  
Old 02-07-11, 14:38
rangupt rangupt is offline
Registered User
 
Join Date: Jan 2008
Posts: 96
Thanks Stealth_DBA

The access plan for all the 3 statements (MAX(auth_id), without Optimize clause and with optimize clause) reveals the exact same estimated cost.

I ran application snapshot and db2pd to collect some information and found out that this particular statement (the originial one with 'Optimize for' clause) has way over 300,000 NumRef and NumExec.
Reply With Quote
  #4 (permalink)  
Old 02-07-11, 14:50
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
What I'm wondering is why DB2 chooses an index access for a table with just 500 rows. If the table doesn't have many columns (which I don't know), a table scan may be an option as well. Do you have current statistics?
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 02-07-11, 14:56
rangupt rangupt is offline
Registered User
 
Join Date: Jan 2008
Posts: 96
Quote:
Originally Posted by stolze View Post
Do you have current statistics?
Thats another thing I don't understand. The table as i said is quite small less than 500 rows. Yes the statistics were updated over the weekend.
Reply With Quote
  #6 (permalink)  
Old 02-07-11, 14:58
rangupt rangupt is offline
Registered User
 
Join Date: Jan 2008
Posts: 96
Quote:
Originally Posted by stolze View Post
If the table doesn't have many columns (which I don't know)
The table has 8 columns auth_id being the primary key.
Reply With Quote
  #7 (permalink)  
Old 02-07-11, 17:07
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
The only column being selected is auth_id, which is in the index for the PK and there is no WHERE clause. So DB2 can satisfy the query with "index only" access, although it will scan all the leaf pages of the index instead of using the b-tree.
__________________
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
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