SQL Server Performance

Distinct and Group by

Discussion in 'Getting Started' started by Ramachandran, Jan 20, 2008.

  1. Ramachandran New Member

    It's simple question if a query have both distinct and group by (it may be a stupid one). Is it have any performance draw back? Which one will be used to get differenct values.
    select distinct column1, column2 from table group by column1, column2
  2. Jack Vamvas Member

    The Group By and DISTINCT should give you the same query plan
  3. satya Moderator

    Though you have got the answer I would like to refer you to review Estimated execution plan from the query analyzer next time when you would like to see such plans.
  4. Madhivanan Moderator

    [quote user="Ramachandran"]
    It's simple question if a query have both distinct and group by (it may be a stupid one). Is it have any performance draw back? Which one will be used to get differenct values.
    select distinct column1, column2 from table group by column1, column2
    [/quote]
    When you use distinct you dont need group by and vice versa [:)]
    Somewhere I read group by is faster than Distinct for millions of rows
  5. FrankKalis Moderator

    In most cases you will find that identical queries plans are generated for both approaches, however to me it is more natural and logical to use a DISTINCT when I need to determine different values and a GROUP BY when I need to perform some aggregation.
  6. satya Moderator

Share This Page