• chriscoates (6/27/2012)


    Perhaps we are at cross purposes Hugo, I meant tht the result set is the same, regardless of uniqueness. Of course if the values are not unique then there's a man-to-many relationship that I'm sure would make my query cost soar to determine the DISTINCT results, but the output would surely be the same?

    Am I missing something else obvious here?

    When comparing two queries for performance, the first thing you must consider is whether they are equivalent. Not just for one set of data, but for each possible set of data.

    In the case of this question, the queries happen to produce the same result for the given data, but will give different results with other data, so they are not equivalent, and you cannot replace one with the other in a production system. If you add constraints to ensure that they are equivalent, the optimizer might (I didn't test this, and have no time for it either) well produce identical plans for the two queries. Without those constraints, it can't produce identical plans, since (depending on the data) it might have to return different results.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/