Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 14th, 2006, 06:25 PM
Peggy Wu
Guest
 
Posts: n/a
Default 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: rtomato@ms40.url.com.tw






  #2  
Old March 15th, 2006, 04:15 AM
ShengTC@gmail.com
Guest
 
Posts: n/a
Default Re: SQL to get the max or min record which one is better?!

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

  #3  
Old March 15th, 2006, 04:15 AM
ShengTC@gmail.com
Guest
 
Posts: n/a
Default Re: SQL to get the max or min record which one is better?!

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

  #4  
Old March 15th, 2006, 02:35 PM
Phil Sherman
Guest
 
Posts: n/a
Default Re: SQL to get the max or min record which one is better?!

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:[color=blue]
> 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: rtomato@ms40.url.com.tw
>
>
>
>
>
>[/color]
  #5  
Old March 15th, 2006, 03:55 PM
Peggy Wu
Guest
 
Posts: n/a
Default Re: SQL to get the max or min record which one is better?!

Thanks for the fast reply!! This helped greatly.
I'll try it !!
<ShengTC@gmail.com>
???????:1142395489.094020.155170@v46g2000cwv.googl egroups.com...
I think if you create a index that "allows reverse scan", then it
makes no difference either way.


  #6  
Old March 15th, 2006, 03:55 PM
Peggy Wu
Guest
 
Posts: n/a
Default Re: SQL to get the max or min record which one is better?!

Thanks for the fast reply!! This helped greatly.
I'll try it


"Phil Sherman" <psherman@ameritech.net>
???????:OtVRf.6475$%m4.3047@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:[color=blue]
> 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: rtomato@ms40.url.com.tw
>
>
>
>
>
>[/color]


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles