Is is more efficient to use SELECT DISTINCT or GROUP BY to perform the same function?

Question

The following two queries produce the same results. Is one or the other versions of these two queries more efficient than the other?

SELECT DISTINCT productcode
FROM sales

SELECT productcode
FROM sales
GROUP BY productcode

Answer

The goal of both of the above queries is to produce a list of distinct product codes from the sales table. The first query uses SELECT DISTINCT to accomplish this task, and the second query uses GROUP BY.

If you were to run these two identical queries in Query Analyzer, with the Execution Plan option turned on, you would find that not only are the results identical, but the Execution Plans are identical. The SQL Server Query Optimizer has the ability to “decipher” each query, determining what the ultimate results are, and to produce the most efficient Execution Plan possible.

So the answer to your question is that there is no performance advantage to using one form of the query over the other.




Related Articles :

  • No Related Articles Found

No comments yet... Be the first to leave a reply!

Software Reviews | Book Reviews | FAQs | Tips | Articles | Performance Tuning | Audit | BI | Clustering | Developer | Reporting | DBA | ASP.NET Ado | Views tips | | Developer FAQs | Replication Tips | OS Tips | Misc Tips | Index Tuning Tips | Hints Tips | High Availability Tips | Hardware Tips | ETL Tips | Components Tips | Configuration Tips | App Dev Tips | OLAP Tips | Admin Tips | Software Reviews | Error | Clustering FAQs | Performance Tuning FAQs | DBA FAQs |