473,738 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does FETCH FIRST slow down query?

During a routine performance check using an event monitor, I
discovered a class of query whose performance has me baffled.

The monitor captured:

SELECT * FROM EWM_CASE fetch first 1 rows only

It took 14 seconds of CPU time to execute. After looking up the
documentation on the FETCH FIRST notation I find "Limiting the result
table to the first integer rows can improve performance. The database
manager will cease
processing the query once it has determined the first integer rows."

I did some validation tests. From UNIX command line, I issued:

db2 -x "SELECT * FROM EWM_CASE fetch first 1 rows only"

and

db2 -x "SELECT * FROM EWM_CASE"|head -1

I got the same row back in both cases but times were drastically
different (13.86 secs vs. .002 sec).

When asked to explain this behavior, I was at a loss. Any clues from
the group?

(AIX 4.3, DB2 7.2 FP11)

Thanks,
Evan
Nov 12 '05 #1
8 21124
"Evan Smith" <es********@hot mail.com> wrote in message
news:23******** *************** ***@posting.goo gle.com...
During a routine performance check using an event monitor, I
discovered a class of query whose performance has me baffled.

The monitor captured:

SELECT * FROM EWM_CASE fetch first 1 rows only

It took 14 seconds of CPU time to execute. After looking up the
documentation on the FETCH FIRST notation I find "Limiting the result
table to the first integer rows can improve performance. The database
manager will cease
processing the query once it has determined the first integer rows."

I did some validation tests. From UNIX command line, I issued:

db2 -x "SELECT * FROM EWM_CASE fetch first 1 rows only"

and

db2 -x "SELECT * FROM EWM_CASE"|head -1

I got the same row back in both cases but times were drastically
different (13.86 secs vs. .002 sec).

When asked to explain this behavior, I was at a loss. Any clues from
the group?

(AIX 4.3, DB2 7.2 FP11)

Thanks,
Evan


Did you try a visual explain to see if the access path is different?
Nov 12 '05 #2
Evan Smith wrote:
During a routine performance check using an event monitor, I
discovered a class of query whose performance has me baffled.

The monitor captured:

SELECT * FROM EWM_CASE fetch first 1 rows only

It took 14 seconds of CPU time to execute. After looking up the
documentation on the FETCH FIRST notation I find "Limiting the result
table to the first integer rows can improve performance. The database
manager will cease
processing the query once it has determined the first integer rows."

I did some validation tests. From UNIX command line, I issued:

db2 -x "SELECT * FROM EWM_CASE fetch first 1 rows only"

and

db2 -x "SELECT * FROM EWM_CASE"|head -1

I got the same row back in both cases but times were drastically
different (13.86 secs vs. .002 sec).

When asked to explain this behavior, I was at a loss. Any clues from
the group?

(AIX 4.3, DB2 7.2 FP11)

Thanks,
Evan


Evan,

You want to also add the clause "OPTIMIZE FOR 1 ROW". For some strange
reason the UNIX optimizer doesn't work out that for itself, unlike the z/OS
optimizer.

Phil
Nov 12 '05 #3
No clue. But how big is the table EWM_CASE. I suppose it is a small table.
I did see some cases - fetch first N rows is very helpful and run faster
when I perform top N queries.

"Evan Smith" <es********@hot mail.com> wrote in message
news:23******** *************** ***@posting.goo gle.com...
During a routine performance check using an event monitor, I
discovered a class of query whose performance has me baffled.

The monitor captured:

SELECT * FROM EWM_CASE fetch first 1 rows only

It took 14 seconds of CPU time to execute. After looking up the
documentation on the FETCH FIRST notation I find "Limiting the result
table to the first integer rows can improve performance. The database
manager will cease
processing the query once it has determined the first integer rows."

I did some validation tests. From UNIX command line, I issued:

db2 -x "SELECT * FROM EWM_CASE fetch first 1 rows only"

and

db2 -x "SELECT * FROM EWM_CASE"|head -1

I got the same row back in both cases but times were drastically
different (13.86 secs vs. .002 sec).

When asked to explain this behavior, I was at a loss. Any clues from
the group?

(AIX 4.3, DB2 7.2 FP11)

Thanks,
Evan

Nov 12 '05 #4
"Fan Ruo Xin" <fa*****@sbcglo bal.net> wrote in message news:<YS******* **********@news svr15.news.prod igy.com>...
No clue. But how big is the table EWM_CASE. I suppose it is a small table.
I did see some cases - fetch first N rows is very helpful and run faster
when I perform top N queries.

After more investigation I can offer the following:

1. Table has 630K rows.
2. Explain Plans for query with and without fetch are same access path
and cost.
3. The output from the event monitor for the "FETCH" query offered
that sorting appears to be going on. Of the 14 CPU seconds consumed,
over 7 are being spent in sort phase.

This has to be a bug, right?
Regards,
Evan
Nov 12 '05 #5
> After more investigation I can offer the following:

1. Table has 630K rows.
2. Explain Plans for query with and without fetch are same access path
and cost.
3. The output from the event monitor for the "FETCH" query offered
that sorting appears to be going on. Of the 14 CPU seconds consumed,
over 7 are being spent in sort phase.

This has to be a bug, right?
Regards,
Evan


Are you sure this is the exact statement? There is no where clause and the
is no order by, etc?

SELECT * FROM EWM_CASE fetch first 1 rows only

Is this inside a cursor? Try adding FOR FETCH ONLY so they new statement is

SELECT * FROM EWM_CASE FOR FETCH ONLY fetch first 1 rows only
Nov 12 '05 #6

"Mark A" <no****@nowhere .com> wrote in message
news:4c******** *******@news.us west.net...
After more investigation I can offer the following:

1. Table has 630K rows.
2. Explain Plans for query with and without fetch are same access path
and cost.
3. The output from the event monitor for the "FETCH" query offered
that sorting appears to be going on. Of the 14 CPU seconds consumed,
over 7 are being spent in sort phase.

This has to be a bug, right?
Regards,
Evan

Are you sure this is the exact statement? There is no where clause and the
is no order by, etc?

SELECT * FROM EWM_CASE fetch first 1 rows only

Is this inside a cursor? Try adding FOR FETCH ONLY so they new statement

is
SELECT * FROM EWM_CASE FOR FETCH ONLY fetch first 1 rows only

Well, where does the SORTING come from? Both sql stmts should use TABLE
SCAN. Anyway it is very interesting, so I try to reproduce it. But failed. I
got the same exec time either using EE or EEE.
Nov 12 '05 #7
>
Well, where does the SORTING come from? Both sql stmts should use TABLE
SCAN. Anyway it is very interesting, so I try to reproduce it. But failed. I
got the same exec time either using EE or EEE.


I'm just as confused as you are. I am including the output from the
DB2 event monitor for the two statements. These were both invoked from
the CLP. One of them shows sorting, the other doesn't. Perhaps one of
the more knowing event monitor gurus out there can see something that
I can't. (Caching doesn't seem to be in effect here either, as I get
similar results when I reverse the order of the operations.)

Here are the two entries:

126) Statement Event ...
Appl Handle: 239
Appl Id: 0A550712.F9CB.0 40726155050
Appl Seq number: 0001

Record is the result of a flush: FALSE
-------------------------------------------
Type : Dynamic
Operation: Close
Section : 201
Creator : NULLID
Package : SQLC2D03
Cursor : SQLCUR201
Cursor was blocking: FALSE
Text : SELECT * FROM db2admin.EWM_CA SE fetch first 1 rows only
-------------------------------------------
Start Time: 07-26-2004 11:51:30.019546
Stop Time: 07-26-2004 11:51:45.155254
Exec Time: 15.135708 seconds
Number of Agents created: 1
User CPU: 13.690000 seconds
System CPU: 0.010000 seconds
Fetch Count: 1
Sorts: 1
Total sort time: 7979
Sort overflows: 1
Rows read: 1877
Rows written: 1991954
Internal rows deleted: 0
Internal rows updated: 0
Internal rows inserted: 0
SQLCA:
sqlcode: 0
sqlstate: 00000

144) Statement Event ...
Appl Handle: 239
Appl Id: 0A550712.F9CB.0 40726155050
Appl Seq number: 0001

Record is the result of a flush: FALSE
-------------------------------------------
Type : Dynamic
Operation: Close
Section : 201
Creator : NULLID
Package : SQLC2D03
Cursor : SQLCUR201
Cursor was blocking: FALSE
Text : SELECT * FROM db2admin.EWM_CA SE
-------------------------------------------
Start Time: 07-26-2004 11:51:45.243032
Stop Time: 07-26-2004 11:51:45.297224
Exec Time: 0.054192 seconds
Number of Agents created: 1
User CPU: 0.000000 seconds
System CPU: 0.000000 seconds
Fetch Count: 208
Sorts: 0
Total sort time: 0
Sort overflows: 0
Rows read: 208
Rows written: 0
Internal rows deleted: 0
Internal rows updated: 0
Internal rows inserted: 0
SQLCA:
sqlcode: 0
sqlstate: 00000
Nov 12 '05 #8
es********@hotm ail.com (Evan Smith) wrote in message news:<23******* *************** *@posting.googl e.com>...

Well, where does the SORTING come from? Both sql stmts should use TABLE
SCAN. Anyway it is very interesting, so I try to reproduce it. But failed. I
got the same exec time either using EE or EEE.


I'm just as confused as you are. I am including the output from the
DB2 event monitor for the two statements. These were both invoked from
the CLP. One of them shows sorting, the other doesn't. Perhaps one of
the more knowing event monitor gurus out there can see something that
I can't. (Caching doesn't seem to be in effect here either, as I get
similar results when I reverse the order of the operations.)

Here are the two entries:

126) Statement Event ...
Appl Handle: 239
Appl Id: 0A550712.F9CB.0 40726155050
Appl Seq number: 0001

Record is the result of a flush: FALSE
-------------------------------------------
Type : Dynamic
Operation: Close
Section : 201
Creator : NULLID
Package : SQLC2D03
Cursor : SQLCUR201
Cursor was blocking: FALSE
Text : SELECT * FROM db2admin.EWM_CA SE fetch first 1 rows only
-------------------------------------------
Start Time: 07-26-2004 11:51:30.019546
Stop Time: 07-26-2004 11:51:45.155254
Exec Time: 15.135708 seconds
Number of Agents created: 1
User CPU: 13.690000 seconds
System CPU: 0.010000 seconds
Fetch Count: 1
Sorts: 1
Total sort time: 7979
Sort overflows: 1
Rows read: 1877
Rows written: 1991954
Internal rows deleted: 0
Internal rows updated: 0
Internal rows inserted: 0
SQLCA:
sqlcode: 0
sqlstate: 00000

144) Statement Event ...
Appl Handle: 239
Appl Id: 0A550712.F9CB.0 40726155050
Appl Seq number: 0001

Record is the result of a flush: FALSE
-------------------------------------------
Type : Dynamic
Operation: Close
Section : 201
Creator : NULLID
Package : SQLC2D03
Cursor : SQLCUR201
Cursor was blocking: FALSE
Text : SELECT * FROM db2admin.EWM_CA SE
-------------------------------------------
Start Time: 07-26-2004 11:51:45.243032
Stop Time: 07-26-2004 11:51:45.297224
Exec Time: 0.054192 seconds
Number of Agents created: 1
User CPU: 0.000000 seconds
System CPU: 0.000000 seconds
Fetch Count: 208
Sorts: 0
Total sort time: 0
Sort overflows: 0
Rows read: 208
Rows written: 0
Internal rows deleted: 0
Internal rows updated: 0
Internal rows inserted: 0
SQLCA:
sqlcode: 0
sqlstate: 00000


The first query taking (14 seconds) seems to have a sort which is
spilling to the disk. I would suggest looking at the explains
(db2exfmt) and have a look at the cost associated with the sort
operator and the one ABOVE that. if the cost increases for the
operator ABOVE the sort, I would suggest increasing the sortheapsz.
....howevever.. .let me know if it worked cause I am still not sure why
the sort is taking place..
Nov 12 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1965
by: Jo | last post by:
Hi, I am using a OleDB provider that connects to ANY datasource - I do not know if these have some nice ID that I can page on. Using the OleDataAdapter FILL method. I then fill the dataset with say 1000 records. If the source table is 6 000 000 rows long there is a problem in that the datareader fetches all 6 000 000 and then returns the 1000 that I asked for. This takes some time if the Provider is on another machine (as it would be
3
2919
by: Franz | last post by:
Hi, If my ProjectTable contains a lot of rows, each time I call the following code, will all the rows be fetched? Or only those 5 records for that page are fetched only? /* -- code start -- */ OdbcConnection connection = new OdbcConnection("DSN=Whatever"); connection.Open(); string query = "SELECT * FROM ProjectTable";
26
17211
by: GreatAlterEgo | last post by:
Hi, This is my query which is embedded in a COBOL program. EXEC SQL SELECT DATE, AGE, DURATION, AMT INTO :LDATE, :L.AGE, :L.DURATION, :L.AMT FROM TAB1 WHERE CODE = :KEY.CODE AND SET = :KEY.SET AND DATE <= :KEY.DATE
5
1843
by: cdtsly | last post by:
Hi i have a table with all value at 4 i select all lines in a fetch i update one with a value of 7 i update all the row in the fetch with a value 5 the result is that all my row are at 5 and the only line which should be at 7 is at 5 too. i see that the problem is at the first mysql_db_query, and i don t know
9
19156
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ------------------------- SELECT * FROM ( Select x.event_date From x FULL OUTER JOIN y ON x.event_date = y.event_date
23
5294
by: PeterOut | last post by:
If I had code like this. unsigned short usLimit=10 int a, i; for (i=0; i<(int)usLimit; ++i) { a=(int)usLimit; }
3
2016
by: salad | last post by:
I have an A97 application that is NOT split on a network. It is used by 15+ folks continually. It is quick and fast. I split it several years ago and had to merge it together again after the folks rebelled at the slow speed resulting from the split. I have to bite the bullet and split it now. Of course, it is much slower. In one form that opens as a continous form, the data looks like it is being repainted as it displays...you can...
15
2061
by: jim | last post by:
Maybe I'm missing something, but it doesn't look like Microsoft writes a lot of apps in .Net (although they certainly push it for others). What does MS write using pure .Net? If applications like Symantec's antivirus, NeatReciepts or Franklin Covey's PlanPlus for Windows is any guide, .Net applications are slow and clunky. But, maybe the developers of these apps simply don't know how to write a decent app with .Net.
5
4230
by: Lennart | last post by:
Here is a problem I would like some opinions on. I have a stored procedure (I suspect that the problem exists for other procedures as well, but I haven't verified it). First time it runs (each day) it takes 8-10 minutes to run. If I rerun the procedure after a while it runs in 2-4 minutes. The SP doesnt update any data itself, but of course others do but that difference in data is irrelevant for the problem. My first thought was that it...
0
8787
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.