By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,007 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,007 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
I think if you create a index that "allows reverse scan", then it
makes no difference either way.

Mar 15 '06 #2

P: n/a
I think if you create a index that "allows reverse scan", then it
makes no difference either way.

Mar 15 '06 #3

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.