Predict output

  • I do NOT think that "union (distinct)" always entails an "order by", neither theoretically (as several others have pointed out), neither practically.

    While I've done no testing, there may be cases where SQLServer resorts to hash comparisons insead of ordering to eliminate duplicates. You're probably 99% safe assuming it will "order by" a single int field union. In more complex situations, I wouldn't bet (yet).

    Is there any written guarantee from MS that any version of MSSQL always returns ordered results out of "union (disjoint)" queries ? Or are you people just wildguessing ?

  • As others have mentioned, a sort is not the only option, and the NULL may or may not come first... No one actually showed this yet, though, so here you go:

    SELECT 1 UNION SELECT NULL UNION SELECT '1'

    OPTION (HASH UNION)

    --
    Adam Machanic
    whoisactive

  • Nice question.

    The execution plan for slightly modified query

    SELECT 2 UNION

    SELECT 1 union

    SELECT NULL UNION

    SELECT '1' union

    SELECT '2'

    shows that Merge Join operators are used to merge inputs from particular SELECT queries. BOL says that Merge Join operator may introduce sort as it requires inputs to be sorted. Since the Selects return only single values, they are 'sorted' and we don't see explicit sort in execution plan.

    Interesting thing happens when I modify the query to following code:

    SELECT 2 UNION

    select a

    from

    (

    SELECT '2' a UNION ALL

    SELECT 1

    ) q UNION

    SELECT NULL UNION

    SELECT '1'

    The Merge Joins are replaced by Concatenation operator and explicit Distinct Sort is used to remove duplicates. If I add option (merge union) to above query, the sort operation is added on the input of the Merge Join implemening union SELECT 2 UNION

    select a

    from ....

    I wonder if it is possible to get output 1, NULL, 2 or 2, NULL, 1. The Hash Match operator that is used when option (hash union) is used apparently sorts data anyway, just NULLs are added to the end of the output.

    Piotr

    ...and your only reply is slàinte mhath

  • Piotr Rodak (11/22/2008)


    The Hash Match operator that is used when option (hash union) is used apparently sorts data anyway, just NULLs are added to the end of the output.

    Piotr

    The hash union sorts the hash values (and sometimes sorts the values within a set sharing a hash value); presumably the hash value currently used for NULL sorts high. There's no guarantee that it will sort high in the next version. There's no guarantee that the optimier won't chose some other duplicate elimination algorithm which hasn't been mentioned yet when it thinks its appropriate for the dataset, which may end up with a different order.

    Tom

    Tom

  • Good Question 🙂

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

Viewing 5 posts - 16 through 19 (of 19 total)

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