473,388 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

SQL to get the max or min record which one is better?!

dear all,
I am a programmer ,coding in IBM COBOL and the DB is DB2 V6 on OS/390.
As below, for the performance issue, Does anyone know which one is better
??

To get a record that field A has a maximum value,
either I could declare a cursor to select the qualified records order by
field A descending,
then fetch the 1st record.
(exec-sql
declare cur01
select * from table B
order by field A desc
exec-end
.................
exec-sql
fetch cur01 -------> fetch only 1 time
into :host-vrecord
exec-end)
or get the max(A) from this table and select the record that field
a equal maximum value
(exec-sql
select * from table B
where fieldA = ( select max(field A) from table B)
exec-end)

thank you for your kindly help,

Sincerely yours,
Peggy Wu
mailto: rt*****@ms40.url.com.tw


Mar 14 '06 #1
5 4181
I think if you create a index that "allows reverse scan", then it
makes no difference either way.

Mar 15 '06 #2
I think if you create a index that "allows reverse scan", then it
makes no difference either way.

Mar 15 '06 #3
An ascending index allowing reverse scans (see ShengTC's reply) or a
descending index on column A will give the best performance. You should
use the singleton select:
select * from B into :host-vrecord
where col A = (select max(col a) from b)
This will minimize the number of communications between your program and
DB2, decreasing CPU costs. This will work only if column a is unique. If
it isn't, you will have to use a cursor to fetch the rows.

If you have only an ascending index on column A then:
1. "Select * ... order by ... desc" will sort the entire table into
descending sequence then start passing you the data rows.
2. Obtaining the MAX value should scan the index. Using the value for a
single row retrieval will avoid any sorting. The SQL statement for a
descending index (above) should work well.

If there's no index, then you will have to either sort the entire table
or make two scans of the table - one to get the MAX value, one to
retrieve the data. Again, the SQL statement for a descending index
(above) should work well. The optimizer may be smart enough to recognize
that this could be done in one scan and do it that way.

Don't forget to use the EXPALIN facility to determine how the optimizer
will attempt to do each of the retrievals.

Philip Sherman

Peggy Wu wrote:
dear all,
I am a programmer ,coding in IBM COBOL and the DB is DB2 V6 on OS/390.
As below, for the performance issue, Does anyone know which one is better
??

To get a record that field A has a maximum value,
either I could declare a cursor to select the qualified records order by
field A descending,
then fetch the 1st record.
(exec-sql
declare cur01
select * from table B
order by field A desc
exec-end
.................
exec-sql
fetch cur01 -------> fetch only 1 time
into :host-vrecord
exec-end)
or get the max(A) from this table and select the record that field
a equal maximum value
(exec-sql
select * from table B
where fieldA = ( select max(field A) from table B)
exec-end)

thank you for your kindly help,

Sincerely yours,
Peggy Wu
mailto: rt*****@ms40.url.com.tw


Mar 15 '06 #4
Thanks for the fast reply!! This helped greatly.
I'll try it !!
<Sh*****@gmail.com>
???????:11**********************@v46g2000cwv.googl egroups.com...
I think if you create a index that "allows reverse scan", then it
makes no difference either way.
Mar 15 '06 #5
Thanks for the fast reply!! This helped greatly.
I'll try it
"Phil Sherman" <ps******@ameritech.net>
???????:Ot*****************@newssvr33.news.prodigy .com...
An ascending index allowing reverse scans (see ShengTC's reply) or a
descending index on column A will give the best performance. You should
use the singleton select:
select * from B into :host-vrecord
where col A = (select max(col a) from b)
This will minimize the number of communications between your program and
DB2, decreasing CPU costs. This will work only if column a is unique. If
it isn't, you will have to use a cursor to fetch the rows.

If you have only an ascending index on column A then:
1. "Select * ... order by ... desc" will sort the entire table into
descending sequence then start passing you the data rows.
2. Obtaining the MAX value should scan the index. Using the value for a
single row retrieval will avoid any sorting. The SQL statement for a
descending index (above) should work well.

If there's no index, then you will have to either sort the entire table
or make two scans of the table - one to get the MAX value, one to
retrieve the data. Again, the SQL statement for a descending index
(above) should work well. The optimizer may be smart enough to recognize
that this could be done in one scan and do it that way.

Don't forget to use the EXPALIN facility to determine how the optimizer
will attempt to do each of the retrievals.

Philip Sherman

Peggy Wu wrote:
dear all,
I am a programmer ,coding in IBM COBOL and the DB is DB2 V6 on OS/390.
As below, for the performance issue, Does anyone know which one is
better
??

To get a record that field A has a maximum value,
either I could declare a cursor to select the qualified records order
by
field A descending,
then fetch the 1st record.
(exec-sql
declare cur01
select * from table B
order by field A desc
exec-end
.................
exec-sql
fetch cur01 -------> fetch only 1 time
into :host-vrecord
exec-end)
or get the max(A) from this table and select the record that
field
a equal maximum value
(exec-sql
select * from table B
where fieldA = ( select max(field A) from table B)
exec-end)

thank you for your kindly help,

Sincerely yours,
Peggy Wu
mailto: rt*****@ms40.url.com.tw


Mar 15 '06 #6

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

Similar topics

9
by: Mark | last post by:
I have a working PHP/MySQL application used for data entry. The data entry screen includes a "Save" button. The PHP code for this button looks like this: if (isset($_POST)) { if ($_POST ==...
1
by: Joe Bond | last post by:
Hi. I have a simple MS Access 2000 form in which I enter some customer data. When the address field is entered I need to see if a duplicate record exists. I need to know this *right away* before...
5
by: Ilan Sebba | last post by:
When it comes to adding records in related tables, Access is really smart. But when I try to do the same using ADO, I am really stupid. Say I have two parent tables (eg Course, Student) and one...
3
by: Steven Stewart | last post by:
Hi there, I have posted about this before, but yet to arrive at a solution. I am going to try to explain this better. I have an Employees table and a Datarecords table (one-to-many...
15
by: Steve | last post by:
I have a form with about 25 fields. In the BeforeUpdate event of the form, I have code that sets the default value of each field to its current value. For a new record, I can put the focus in any...
20
by: MS | last post by:
Access 97 I want to requery the data being displayed on a form, then I want to return to the record I was in. Why doesn't this code work? Private Sub CmdRefsh_Click()
2
by: Blankdraw | last post by:
.... somewhere, a newbie is dying ... Is there anybody out there who can help me get the right input for the following segment? I am trying to read entire records of 5 (2-digit) integers at a...
2
by: Mark Reed | last post by:
Hi All, I have created a multi-user application at work which is working perfectly apart from a small problem which I believe to more a of a user issue (maybe some will set me straight on that...
48
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) *...
3
prn
by: prn | last post by:
Hi folks, I've got something that's driving me crazy here. If you don't want to read a long explanation, this is not the post for you. My problematic Access app is a DB for keeping track of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.