Discussion:
SQL select MAX or TOP 1
(too old to reply)
Kevin Clark
2011-02-11 15:34:33 UTC
Permalink
I am trying to do an SQL select on a table to determine the highest
entry based upon a certain field. The field is indexed. I have tried
both:

select number from file order by number desc top 1

and

select max(number)

neither one of these queries is optimized and they both take a long
time.

Any suggestions on a different query of any index optimization I might
try.

Regards,
Kevin Clark
Dan Freeman
2011-02-11 16:52:21 UTC
Permalink
Post by Kevin Clark
I am trying to do an SQL select on a table to determine the highest
entry based upon a certain field. The field is indexed. I have tried
select number from file order by number desc top 1
and
select max(number)
neither one of these queries is optimized and they both take a long
time.
Any suggestions on a different query of any index optimization I might
try.
Regards,
Kevin Clark
If the field is indexed, your fastest route is

SET ORDER
GO BOTTOM

Dan
Gene Wirchenko
2011-02-11 20:02:26 UTC
Permalink
On Fri, 11 Feb 2011 07:34:33 -0800 (PST), Kevin Clark
Post by Kevin Clark
I am trying to do an SQL select on a table to determine the highest
entry based upon a certain field. The field is indexed. I have tried
How is the field indexed? It should be, as I understand, just
number
str() and such will mess things up.
Post by Kevin Clark
select number from file order by number desc top 1
and
select max(number)
I am not sure what you mean by your second command. I would do
select max(number) as themax from file
The order and top are unnecessary.
Post by Kevin Clark
neither one of these queries is optimized and they both take a long
time.
Any suggestions on a different query of any index optimization I might
try.
Sincerely,

Gene Wirchenko

Continue reading on narkive:
Loading...