Using Distinct or group by

  • Hi,

    Another day,another query.

    I know that,we should not use distinct clause frequently.

    Because using the DISTINCT clause will result in some performance degradation, you should use this clause only when it is necessary.

    Instead I use Group by clause is it ok?Result are same.

    what about performance ?

    [font="Verdana"]Regards
    Kumar Harsh[/font]

    :unsure:

  • You can see in the execution plan that both options are executed alike.

    But only use DISTINCT or GROUP BY when required, because they can cause real performance hits.

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • DISTINCT and GROUP BY are both methods of aggregating data. In general terms, neither is going to perform better than the other one.

    In general practice, you need to structure your storage such that you don't have lots and lots of duplicate data requiring you to aggregate it in order to return valid result sets. That, or you need to examine your code to ensure you're accessing the data correctly, using all the columns in the key and in foreign keys, etc. It's not that you should only use aggregation sparingly in your system, you should only need to use it sparingly (or specifically when generating aggregation for reports, etc.).

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply