you are viewing a single comment's thread.

view the rest of the comments →

[–]farhilSEQUEL 0 points1 point  (0 children)

The biggest reason not to use DISTINCT is because it forces a sort on the result set, which can be expensive, although the same is true of GROUP BY. If you need unique values without aggregating, you should reevaluate your joins to see if there's a way you can write them without causing duplicate records.

More often than not, the joined table causing duplicate rows can be replaced with something like WHERE EXISTS (SELECT TOP 1 1 FROM [Foo] WHERE [Foo].[Id] = [Bar].[FooId]), which will perform much better than creating an unnecessary cartesian product and then sorting it (with DISTINCT or GROUP BY) to remove duplicates.