SQL Server UNION - What is the default ORDER BY Behaviour

前端 未结 6 2001
生来不讨喜
生来不讨喜 2020-11-27 20:38

If I have a few UNION Statements as a contrived example:

SELECT * FROM xxx WHERE z = 1
UNION 
SELECT * FROM xxx WHERE z = 2
UNION
SELECT * FROM xxx WHERE z =         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 21:08

    If you care what order the records are returned, you MUST use an order by.

    If you leave it out, it may appear organized (based on the indexes chosen by the query plan), but the results you see today may NOT be the results you expect, and it could even change when the same query is run tomorrow.

    Edit: Some good, specific examples: (all examples are MS SQL server)

    • Dave Pinal's blog describes how two very similar queries can show a different apparent order, because different indexes are used:

      SELECT ContactID FROM Person.Contact
      SELECT *         FROM Person.Contact
      
    • Conor Cunningham shows how the apparent order can change when the table gets larger (if the query optimizer decides to use a parallel execution plan).

    • Hugo Kornelis proves that the apparent order is not always based on primary key. Here is his follow-up post with explanation.

提交回复
热议问题