When the performance of Distinct and Group By are different?

后端 未结 2 1408
谎友^
谎友^ 2020-12-09 03:21

I know in simple queries the performance and execution plans of the Distinct and Group By are almost the same.

e.g.

SELECT Name FROM NamesTable GROUP         


        
相关标签:
2条回答
  • 2020-12-09 04:10

    Here are 2 examples, one for producing a different result and the other for a different performance:

    Example for producing different performance

    And the second example:

    Example for producing different result

    0 讨论(0)
  • 2020-12-09 04:13

    If you include a calculated value in the field list you will see a difference in the execution plan.

    select Value,
           getdate()
    from YourTable
    group by UnitID
    
    select distinct
           Value,
           getdate()
    from YourTable
    

    The group by query aggregates before it computes the scalar value. The distinct query computes the scalar value before the aggregate.

    0 讨论(0)
提交回复
热议问题