Why does SQL Server 2008 order when using a GROUP BY and no order has been specified?

后端 未结 4 714
无人共我
无人共我 2021-01-06 05:23

I\'m running into a very strange issue that I have found no explanation for yet. With SQL Server 2008 and using the GROUP BY it is ordering my columns without any ORDER BY s

4条回答
  •  暖寄归人
    2021-01-06 06:18

    This is a DB optimisation facet. The SQL Engine generally has to parse data based in order of the grouped column. Rather than waste time afterwards it leaves the dataset in whatever order parsed.

    There are situations where this will not be the case (especially if using aggregate functions). Using the ORDER BY command obviously overrides this functionality (and consequently implies a tiny bit extra load, not enough to worry about in all but the most extreme cases though).

提交回复
热议问题