optimize for n rows vs fetch first n rows.

RDBMS from IBM and IBM's Hierarchical DBMS and Transaction Manager (IMS DC).
Post Reply
Omi
Registered Member
Posts: 10
Joined: Sat Apr 26, 2014 5:46 pm

optimize for n rows vs fetch first n rows.

Post by Omi »

Hi,

Is there any difference in performance if we use optimize for n rows vs fetch first n rows in DB2 on zOS? I found a link on DB2 but that applies to Db2 on windows: http://pic.dhe.ibm.com/infocenter/db2lu ... 55223.html.
User avatar
Anuj Dhawan
Founder
Posts: 2801
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: optimize for n rows vs fetch first n rows.

Post by Anuj Dhawan »

The concept explained in the link is applicable to DB2 on zOS as well. Just to reiterate - Optimize for x rows tells DB2 that out of the entire result set, you want to get the first x rows as fast as possible! This tells DB2 sub-system to favor INDEXes that eliminate sorts, favor nested loops JOINs and siblings.

Fetch first x rows only, OTOH, tells DB2 to automatically close the cursor after x rows. So, fetch first x rows only automatically implies optimize for x rows.

Sometimes, you might want to use both, for example, if you need to fetch 1000 rows, but you need the first 10 very quickly to display on user interface, you can specify

Code: Select all

select
...
FETCH FIRST FIRST 1000 ROWS ONLY
OPTIMIZE FOR 10 ROWS
Hope this helps.
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
Omi
Registered Member
Posts: 10
Joined: Sat Apr 26, 2014 5:46 pm

Re: optimize for n rows vs fetch first n rows.

Post by Omi »

Anuj, thanks. I got to understand it better now.
User avatar
Anuj Dhawan
Founder
Posts: 2801
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: optimize for n rows vs fetch first n rows.

Post by Anuj Dhawan »

You're welcome! :)
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM DB2 and IMS DB/DC”