Connecting Tech Pros Worldwide Help | Site Map

max or not max?

Mark
Guest
 
Posts: n/a
#1: Nov 14 '06
Hello,

I have a question about the use of the function "max" and the use of index.
I have a table with primary key "nr" (DataType Integer).

when i do

desc select max(nr) from data_sales

the result is:

id: 1
select_type: SIMPLE
table: NULL
type: NULL
possible_keys NULL
key: NULL
key_len: NULL
ref: NULL
rows: NULL
Extra Select tables optimized away

if i rewrite my query to:

desc select nr from data_sales order by nr desc limit 1

the result is:
.....
table: data_sales
type: index
possible_keys: NULL
key: PRIMARY
....
rows: Using index

Does this mean the second query is better? or is "max" otherwise optimized?

Thanks!


Aggro
Guest
 
Posts: n/a
#2: Nov 14 '06

re: max or not max?


Mark wrote:
Quote:
desc select max(nr) from data_sales
Quote:
desc select nr from data_sales order by nr desc limit 1
Quote:
Does this mean the second query is better? or is "max" otherwise optimized?
AFAIK max() is optimized internally. Use it instead of custom queries.
Closed Thread