Which is better: Distinct or Group By

后端 未结 6 992
南方客
南方客 2021-02-07 12:45

Which is more efficient?

SELECT  theField
FROM    theTable
GROUP BY theField

or

SELECT  DISTINCT theField
FROM    theTable
         


        
6条回答
  •  -上瘾入骨i
    2021-02-07 13:14

    In your example, both queries will generate the same execution plan so their performance will be the same.

    However, they both have their own purpose. To make your code easier to understand, you should use distinct to eliminate duplicate rows and group by to apply aggregate operators (sum, count, max, ...).

提交回复
热议问题