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.

]]>

Leave a comment

Your email address will not be published.