Question

Query A

SELECT Distinct ord_no FROM Orders 
ORDER BY ord_no

Query B

SELECT ord_no FROM Orders 
GROUP BY ord_no
ORDER BY ord_no

In Orders tabel, ord_no is varchar type and has duplicate. Here it's a composite key with a identity column.

  1. May I know which query is better?
  2. How could we check the query performance using MS SQL Server 2008 R2 (express version)?
Was it helpful?

Solution 2

Check the execution plans for both queries. It's very likely that they will be the same, especially with such a simple query (you'll probably see a stream aggregate operator, doing the grouping, in both cases).

If the execution plans are the same, then there is no (statistically significant) difference in performance between the two.

Having said that, use group by instead distinct whenever in doubt.

OTHER TIPS

You can see the amount of time each query takes in milli seconds on the SQL profiler. From management studio, go to Tools --> Profiler and start a trace on your DB. The run your queries. You can see the duration they took. Mind you, you'll need to have considerable amount of data to see the difference. You can use SQL Express Profiler if you are not on the full blown version of SQL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top