加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

SQL order by子句是否保证稳定(按标准)

发布时间:2021-01-28 04:43:24 所属栏目:编程 来源:网络整理
导读:我正在使用以下查询来查询在两列上有订单的DB. SELECT a,b,c from Table1 Order By a asc,b asc; 我的问题是,排序是否保证稳定(按标准).虽然它没有任何意义,但不稳定,但我问这个,因为我在网上读到了 The standard does not prevent the use of a stable sort,

我正在使用以下查询来查询在两列上有订单的DB.

SELECT a,b,c from Table1 Order By a asc,b asc;

我的问题是,排序是否保证稳定(按标准).虽然它没有任何意义,但不稳定,但我问这个,因为我在网上读到了

The standard does not prevent the use of a stable sort,but it also
does not require it.

解决方法

这种排序不能保证稳定.我认为SQL Server documentation对如何实现稳定排序有一个很好的解释:

To achieve stable results between query requests using OFFSET and
FETCH,the following conditions must be met:
The underlying data that is used by the query must not change. That is,either the rows touched
by the query are not updated or all requests for pages from the query
are executed in a single transaction using either snapshot or
serializable transaction isolation. For more information about these
transaction isolation levels,see SET TRANSACTION ISOLATION LEVEL
(Transact-SQL). The ORDER BY clause contains a column or combination
of columns that are guaranteed to be unique.

理解排序不稳定的最简单方法是返回表的定义.表在SQL中本质上是无序的.因此,没有任何顺序可以依靠“稳定性”.

作为第二个考虑因素,可以并行地实施分类.在大多数并行排序中,公共密钥被汇集在一起??而没有关于其原始顺序的信息(除非在排序密钥中显式或隐式地实现).

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读