对于返回超过1个值的SQL select,当Id为GUID时它们如何排序?
我想知道 SQL Server 如何对查询返回的数据进行排序,并且各个表的 Id 列都是 uniqueidentifier 类型。
我在创建所有 GUID 时使用 NHibernate GuidComb 并执行以下操作:
Sheet sheet = sheetRepository.Get(_SheetGuid_); // has many lines items
IList<SheetLineItem> lineItems = sheet.LineItems;
我只是想弄清楚当我执行以下操作时它们将如何排序:
foreach (SheetLineItem lineItem in lineItems)
我无法找到一篇好文章GUID 在订购时由 SQL 进行比较,如果发生这种情况。
I'm wondering how SQL Server orders data that is returned from a query and the Id columns of the respective tables are all of type uniqueidentifier.
I'm using NHibernate GuidComb when creating all of the GUIDs and do things like:
Sheet sheet = sheetRepository.Get(_SheetGuid_); // has many lines items
IList<SheetLineItem> lineItems = sheet.LineItems;
I'm just trying to figure out how they'll be ordered when I do something like:
foreach (SheetLineItem lineItem in lineItems)
I can't see to find a good article on the way GUIDs are compared by SQL when being ordered, if that's what's happening.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GUID 已排序 这样通过ORDER BY。引用文章...
GUIDs are sorted this way by the ORDER BY. Quoting the article...
除非您包含 ORDER BY 子句,否则 SQL Server 不保证结果的任何顺序。它可能看起来一致地以某种顺序返回(例如聚集索引顺序),但您不能确定情况总是如此(例如,如果查询被拆分并在多个线程上执行,当结果组合在一起时,每次执行的顺序可能不同,因为线程可能以不同的顺序完成)。
获得特定顺序的唯一方法是使用 ORDER BY 子句。在 NHibernate 中,这可以通过在映射文件中的包(或等效项)上指定 order-by="..." 来实现。
有关“order-by”的更多信息,请参阅 NHibernate 文档:http://nhibernate。信息/doc/nh/en/index.html
Unless you incude an ORDER BY clause SQL Server doesn't guarantee any order on the results. It may sem to come back in the some order consistently (e.g. clustered index order) but you can't be sure this will always be the case (e.g. if the query is split and executed on multiple threads, when the results are combined, the order may be different on each execution since the threads may complete in different orders).
The only way to get a particular order is to use an ORDER BY clause. In NHibernate, this would be achieved by speifying an order-by="..." on your bags (or equivalent) in your mapping files.
See the NHibernate docs for more info on "order-by": http://nhibernate.info/doc/nh/en/index.html