Site sponsored by: Idera Try Idera’s new SQL admin toolset
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


FAQ Topics

All FAQ's
General DBA
General Developer
DBA Performance Tuning
Developer Performance Tuning
Clustering
Error Messages

Write for Us

Share you SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Policy Based Management in SQL Server 2008
Inside SQL Server Cluster Setup and Troubleshooting Techniques - Part I ...
Configure and Manage Policy Based Management in SQL Server 2008 ...
Using Column Sets with Sparse Columns

More     
 
Latest FAQ's

Cannot Start SQL Server Service
Users are able to connect to report manager but not able ...
Errors when SQL Server Snapshot Replication is Running
How to Display Server Name or IP Address in a Reporting ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Doc 2008
ApexSQL Enforce
Embarcadero Change Manager
SQL Server DBA Dashboard

More     

How can I determine the maximum value for each data? And which option provides the best performance?



One of the most common requirements is to generate a list of, say, customers along with their last order date. Or, generally speaking, a list of distinct values in one table along with a corresponding maximum value from another table. To solve such problems you basically have three different alternatives. Let's have a look at the methods first, and then discuss their respective performance.

All methods are tested against the Northwind sample database.


Alternative 1


SELECT DISTINCT(CustomerID), OrderDate FROM orders t1
WHERE t1.orderdate=
(SELECT MAX(orderdate) FROM orders t2
WHERE t1.customerid=t2.customerid)
ORDER BY t1.customerid





Alternative 2


SELECT DISTINCT (t1.CustomerID), t1.OrderDate FROM orders t1 INNER JOIN
(SELECT customerid, MAX(orderdate) as Maxdate FROM orders GROUP BY customerid) t2
ON t1.customerid = t2.customerid
AND t1.orderdate = t2.MAXdate
ORDER BY t1.customerid




Alternative 3


SELECT DISTINCT(CustomerID), OrderDate
FROM orders t1
WHERE t1.orderdate IN
(
SELECT top 1 t2.orderdate
FROM orders t2
WHERE t2.customerid = t1.customerid
ORDER BY t2.orderdate DESC
)
ORDER BY t1.customerid



All methods generate an identical output, which is not shown here. So which one is preferable? A look at the output of SET STATISTICS IO reveals some very interesting insights.

(89 row(s) affected)

Table 'Orders'. Scan count 1, logical reads 21, physical reads 0, read-ahead reads 0.

(89 row(s) affected)

Table 'Orders'. Scan count 1, logical reads 21, physical reads 0, read-ahead reads 0.

(89 row(s) affected)

Table 'Orders'. Scan count 825, logical reads 130143, physical reads 0, read-ahead reads 0.



To say the least, Alternative 3 is not a real option for a high-performance environment. But that's not really surprising when you take a closer look at what the last method does. For each distinct CustomerID's in the Orders table, it must scan the Orders table for the latest entry in OrderDate. There are only 89 distinct CustomerID in that table.

So, we can rule out Alternative 3. That leaves us with 2 other methods. Is there one method superior to the other? A look at the output of SET STATISTICS IO tells us, both produce equal IO. A look at the execution plan shows that for both methods an identical plan is generated. So, internally, both queries are treated the same way by SQL Server. Since there is no differences between alternative 1 and 2, you might want to use that one which you feel more comfortable with or the one, which "looks better" to you.








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved