473,772 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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....

Jul 23 '05 #1
2 1316
Stu
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

Jul 23 '05 #2

"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:d8******** ***********@new s.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....

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1873
by: loquak | last post by:
Greets. I wonder which way would be the best for holding some state flags in terms of memory use and efficiency. a) struct FLAGS { bool flag1 : 1; bool flag2 : 1;
7
5334
by: SQLDBA | last post by:
I am in the process of evaluating some SQL Performance Monitoring /DBA tool to purchase (For SQL Server 2000). I have the following list of software that I came across and have to finalize which one to recomend for purchase by my company. Quest Central® for SQL Server Performance Center by Embarcadero (dbartisan) Performance analysis by BMC Software solutions SQL Server Management by netIQ
14
15049
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in a comma-delimited string or string array. I don't want it to be a string because I want to overload this function, and it's sister already uses a string input parameter. Now if I define the function to take in a string array, it solves my...
3
1388
by: Balaji Kannan | last post by:
Hi, In dot net during component development i have used some member variables in the class file. Inside the class i have used the member declaration and the instant handling in the following way. In the constructor i have created the connection instant in the following way
4
1797
by: Lerp | last post by:
Hi all, With regards to calling data from a database and filling in an editing form based on some query, which is the best (least intensive on processor) method for assigning the returned results to a variable. I have been using a couple of ways so far in my project.: 1. strFirst=objDR("Fname") 2. strFirst=objDR.GetOrdinal("Fname")
9
1781
by: Luke Vogel | last post by:
Hi all. This is a bit of a newbie type question. I am trying to figure out what is the best way to connect to a database; ado.net, odic others? I've found a couple of examples that show you how to connect to the "Northwind" database, using oledb??
7
20013
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low cost. STL vector is suitable for removing form tail? or it is as costly as removing from middle? any other std container to serve this purpose? (note , I dont need linked list implementation of any container, as I want random access)
2
1396
by: tulasikumar | last post by:
hi all, i have one doubt regarding queries and stored procedures.i am doing asp.net project,i had written both query based retriving and stored procudures.I want know about which one was giving best performance.and one more is when i am passinf sql statement or stored procedure to sqlserver how these to executed internally. please tell me the answer these queries thanks in advance --
3
3711
by: samadams_2006 | last post by:
Hello, I'm interested in taking the following exam for an upcoming job. Exam 70-315: Developing and Implementing Web Applications with Microsoft Visual C#â„¢ .NET and Microsoft Visual Studio .NET. Since the job is being offered soon I need to know the "quickest" and "best" way to prepare for this exam. Are there any books, practice exams, etc. that are considered "THE BEST"?
20
3094
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in like 65% of the time of the second routine. Yet both do the same thing. However, the second one seems better in terms of the way the code is written since it helps encapsulate the transformation in the inner loop better making it easier to read,...
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.