Unix Technical Forum

Which has the best performance?

This is a discussion on Which has the best performance? within the SQL Server forums, part of the Microsoft SQL Server category; --> This: SELECT MAX(TheDate) FROM MyTable or this: SELECT TOP 1 TheDate FROM MyTable ORDER BY TheDate DESC As a ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 08:51 AM
Robin Tucker
 
Posts: n/a
Default Which has the best performance?


This:

SELECT MAX(TheDate) FROM MyTable

or this:

SELECT TOP 1 TheDate FROM MyTable ORDER BY TheDate DESC


As a follow up question to save me having to post, if I want a different
field from the result set of a MAX query, how do I do it? ie. I want the
"Condition" field of the record with the most recent date. I have been
doing it like this:

SELECT TOP 1 Condition FROM MyTable ORDER BY TheDate DESC

but if MAX(TheDate) is quicker, I would like to SELECT TOP 1 Condition ....
where TheDate is the max date...... Hope this makes sense.....

Basically, I'm going to be performing this query nested inside another query
and I want the maximum performance possible (indexes are a different
question), which means trying to avoid table scans....



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 08:51 AM
Stu
 
Posts: n/a
Default Re: Which has the best performance?

You could do

SELECT TOP 1 Condition
FROM MyTable
WHERE TheDate = (SELECT MAX(TheDate) FROM MyTable)

A lot of the performance will depend on your indexes, though.

I hope that helps.

Stu

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 08:51 AM
Greg D. Moore \(Strider\)
 
Posts: n/a
Default Re: Which has the best performance?


"Robin Tucker" <idontwanttobespammedanymore@reallyidont.com> wrote in
message news:d81e60$hpa$1$830fa7a5@news.demon.co.uk...
>
> This:
>
> SELECT MAX(TheDate) FROM MyTable
>
> or this:
>
> SELECT TOP 1 TheDate FROM MyTable ORDER BY TheDate DESC
>
>
> As a follow up question to save me having to post, if I want a different
> field from the result set of a MAX query, how do I do it? ie. I want the
> "Condition" field of the record with the most recent date. I have been
> doing it like this:
>
> SELECT TOP 1 Condition FROM MyTable ORDER BY TheDate DESC
>
> but if MAX(TheDate) is quicker, I would like to SELECT TOP 1 Condition

.....
> where TheDate is the max date...... Hope this makes sense.....


Probably Select MAX, but really the only way to tell is to try it on your
schema and look at the plans, etc.

In addition,as TOP 1 is T-SQL specific, I'd be much more likely to go with
the MAX option.


>
> Basically, I'm going to be performing this query nested inside another

query
> and I want the maximum performance possible (indexes are a different
> question), which means trying to avoid table scans....
>
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:41 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com