MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

question about "fetch 1st n rows only"

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
jctgf
Beginner


Joined: 05 Nov 2006
Posts: 89
Topics: 36

PostPosted: Mon Dec 13, 2010 4:56 am    Post subject: question about "fetch 1st n rows only" Reply with quote

hi,

i have a cursor that will select thousands (or millions) of lines. this is the requirement.

i thought i could limit the number of rows read with a fetch 1st (and prevent the aplication of abending due to a timeout), but then i found the following in the db2 manual:

| If both the FETCH FIRST clause and the ORDER BY clause are specified, the
| ordering is performed on the entire result table prior to returning the
| first n rows.


because i'd have to order the lines, i think now that the fetch 1st will not solve my problem.

my question is: how does fetch 1st work if i do NOT have the order by clause? will db2 stop reading/selecting the rows as soon as it reaches the number of lines specified in the fetch 1st clause?

or, will it read/select all the lines that satify the ''where'' clause - without considering the fetch 1st clause - and only when sending the lines back to the cobol program it will limit the number of lines to what i've specified in the fetch 1st statement?

thanks.
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Mon Dec 13, 2010 10:08 am    Post subject: Re: question about "fetch 1st n rows only" Reply with quote

jctgf wrote:

my question is: how does fetch 1st work if i do NOT have the order by clause? will db2 stop reading/selecting the rows as soon as it reaches the number of lines specified in the fetch 1st clause?

or, will it read/select all the lines that satify the ''where'' clause - without considering the fetch 1st clause - and only when sending the lines back to the cobol program it will limit the number of lines to what i've specified in the fetch 1st statement?

thanks.


Fetch First Acts as the last command in your order of commands in SQL. All the where conditions, grouping, ordering are performed before the fetch first command.

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jctgf
Beginner


Joined: 05 Nov 2006
Posts: 89
Topics: 36

PostPosted: Mon Dec 13, 2010 11:53 am    Post subject: Reply with quote

hey kolusu,
thanks a lot.
i've heard that fetch 1st improves performance a lot, but i wonder why if it only affects the last stage of the select.
it will only improve the performance if i have a very simple ''where'', with no orderby, groupby and so, right?
thanks again.
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Mon Dec 13, 2010 12:23 pm    Post subject: Reply with quote

jctgf wrote:
hey kolusu,
thanks a lot.
i've heard that fetch 1st improves performance a lot, but i wonder why if it only affects the last stage of the select.
it will only improve the performance if i have a very simple ''where'', with no orderby, groupby and so, right?
thanks again.


Not really. Fetch First 'n' rows influences the buffer size. for example if you wanted to find the 50 top earning employees out a 10 million table, without Fetch first you would have to allocate the buffers for 10 million rows where as with fetch first you would only allocate buffers for 50 rows which is very small.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jim haire
Beginner


Joined: 30 Dec 2002
Posts: 140
Topics: 40

PostPosted: Tue Dec 14, 2010 3:54 pm    Post subject: Reply with quote

Order by, group by, distinct and some other clauses cause sorts which take place before the fetch 1st would be performed. So if you were sorting a multi-million row table, the sort would be performed before any rows are fetched. Sorting often is the most time consuming part of a query.

The Fetch 1st could return a different set of rows every time if you don't sort.
Back to top
View user's profile Send private message
jctgf
Beginner


Joined: 05 Nov 2006
Posts: 89
Topics: 36

PostPosted: Tue Dec 14, 2010 5:06 pm    Post subject: Reply with quote

i did a test today.
i run a query that selected 42000 lines, with an orderby clause.
it took 5 seconds or so to run.
then i added a 'fetch 1st 1000 rows only" and to my surprise the time was about the same.
then a changed it to "100 rows only" and the time dropped to 2 secs.
no matter the value i used in the "fetch 1st", i could only see a significant diff is this value was very small.
i wonder why.
thanks.
Back to top
View user's profile Send private message Send e-mail
papadi
Supermod


Joined: 20 Oct 2009
Posts: 594
Topics: 1

PostPosted: Tue Dec 14, 2010 5:30 pm    Post subject: Reply with quote

Possibly because the higher volume causes actual i/o instead of being handled in-core.
_________________
All the best,

di
Back to top
View user's profile Send private message
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Wed Dec 15, 2010 2:50 pm    Post subject: Reply with quote

Another possibility is that DB2 bypassed the sort and used an index to satisfy the ORDER BY requirements.

From the Implementing Indexes section in Chapter 4 of the Administration Guide
Quote:
Using indexes to avoid sorts
DB2 can use indexes to avoid sorts when processing queries with the ORDER BY clause. When a query contains an ORDER BY clause, DB2 looks for indexes that satisfy the order in the query. For DB2 to be able to use an index to access ordereddata, you must define an index on the same columns as specified in the ORDERBY clause.
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Thu Dec 16, 2010 3:57 am    Post subject: Reply with quote

The OPTIMIZE FOR n ROWS clause could/should also be invesitigated.

when one is working with a LARGE table, one should design the indexes appropriately to prevent LARGE result sets that will not be used.
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
jctgf
Beginner


Joined: 05 Nov 2006
Posts: 89
Topics: 36

PostPosted: Thu Dec 16, 2010 7:06 am    Post subject: Reply with quote

hi there,

i wonder if another possibility would be to use a freespace "big enough" when creating the table. it would reduce the chances of having a split of CI because of the inserts and, therefore, avoiding the table to become disorganized.

another thing, please: the primary key of this table is a sequenced number (i mean a number that is incremented everytime a new record is created). in this situation, new records would always be added to the end of the index/table and no split of CI would occur. consequently, the table would always remain physically sorted. is it correct?

thanks.
Back to top
View user's profile Send private message Send e-mail
papadi
Supermod


Joined: 20 Oct 2009
Posts: 594
Topics: 1

PostPosted: Thu Dec 16, 2010 2:08 pm    Post subject: Reply with quote

Quote:
consequently, the table would always remain physically sorted. is it correct?

Not necessarily. . .

Why are you concerned with "CI splits". . .

The problem is the amount of data selected.

Suggest you re-read what DBZ posted.
_________________
All the best,

di
Back to top
View user's profile Send private message
IEFBR14
Beginner


Joined: 13 Aug 2008
Posts: 17
Topics: 0
Location: SYS1.LINKLIB

PostPosted: Wed Dec 29, 2010 7:48 pm    Post subject: Reply with quote

"new records would always be added to the end of the index/table and no split of CI would occur."
As mentioned above why worry about CI splits? ...Now the Index tree may be an issue
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group