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 

Performance of Fetch First Row

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


Joined: 06 Apr 2007
Posts: 5
Topics: 2

PostPosted: Wed Jun 13, 2007 8:16 am    Post subject: Performance of Fetch First Row Reply with quote

I have a program having many Single Select SQL queries, all having Fetch First Row. The Response time for the online program was near to 40 seconds. Someone suggested to Remove all of the Fetch First Row statements and instead handle multiple rows found in the program itself.
Doing that the Response time decreased to one-third (13 seconds).
Can someone throw some light, why with many Fetch first Row queries the program had poor performance.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


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

PostPosted: Wed Jun 13, 2007 8:19 am    Post subject: Reply with quote

Jiten,


Do you have an ORDER BY clause along with FETCH FIRST? If you specify the FETCH FIRST n ROWS ONLY clause with the ORDER BY clause, the ordering is performed on the entire result table before the first n rows are returned.

also I would suggest that you use OPTIMIZE FOR 1 ROW only along with FETCH first

Code:

SELECT column
  into :host-var 
  FROM table             
OPTIMIZE FOR 1 ROW ONLY     
   FETCH FIRST 1 ROW ONLY


Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Jiten
Beginner


Joined: 06 Apr 2007
Posts: 5
Topics: 2

PostPosted: Wed Jun 13, 2007 8:28 am    Post subject: Reply with quote

Hi Kolusu,

I do not have an Order By Clause. I am simply checking the exisiting of record in the table.
To add to the previous post, for one particular query, The COST reduced from 14564 to 4485 with a change from Fetch First Row to handling -811 in the program.
No order by clause is there atleast in this query.
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


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

PostPosted: Wed Jun 13, 2007 8:35 am    Post subject: Reply with quote

deleted by dbzTHEdinosauer, duplicated Kolusu's Post.
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


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

PostPosted: Wed Jun 13, 2007 9:09 am    Post subject: Reply with quote

Jiten,

I would guess that your WHERE predicates were not indexed.

You should spend some time with EXPLAIN before you code your program.

Kolusu's suggestion will be best when the WHERE predicates are indexed.

if they are not indexed and you don't care about howmany, just 0 or > 0,
you can SELECT 1 WHERE EXISTS which would stop the table scan after 1 find.

There are a lot of articles that can be found thru google about this subject, written between 2001 and 2005.

What version of DB2 are you using?

Next time 'someone suggests', ask them why.
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
Jiten
Beginner


Joined: 06 Apr 2007
Posts: 5
Topics: 2

PostPosted: Wed Jun 13, 2007 11:19 am    Post subject: Reply with quote

hi dbzTHEdinosauer,

I am using DB2 Version 8. If you could explain the usage of SELECT 1 WHERE EXISTS....actually I did not get it. Usage of Exists I know, it will stop after getting the first find. But my actual question was why after removing the Fetch First Row the cost reduced.
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


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

PostPosted: Wed Jun 13, 2007 12:09 pm    Post subject: Reply with quote

Jiten,
I would guess that your WHERE predicates were not indexed, thus table scans.

Instead of copying the sql, I will give you the link of an article that really goes into a detailed explanation. This article is dated 2005, so it is not too out-of-date.
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
CICS Guy
Intermediate


Joined: 30 Apr 2007
Posts: 292
Topics: 3

PostPosted: Wed Jun 13, 2007 12:20 pm    Post subject: Reply with quote

Thanks Dick,

Always an adventure following your bread crumbs........
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


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

PostPosted: Wed Jun 13, 2007 1:04 pm    Post subject: Reply with quote

CICS Guy,

i am only sharing what others have left for me. glad they are helpful.
_________________
Dick Brenholtz
American living in Varel, Germany
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