对于返回超过1个值的SQL select,当Id为GUID时它们如何排序?

发布于 2024-08-30 07:23:29 字数 449 浏览 2 评论 0原文

我想知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杀お生予夺 2024-09-06 07:23:29

GUID 已排序 这样通过ORDER BY。引用文章...

  • 0..3 按从左到右的顺序评估,并且不太重要,然后
  • 4..5 按从左到右的顺序评估,然后
  • 6..7 按从左到右的顺序评估,然后
  • 8..9 按从右到左的顺序计算,然后
  • A..F 按从右到左的顺序计算,并且是最重要的

GUIDs are sorted this way by the ORDER BY. Quoting the article...

  • 0..3 are evaluated in left to right order and are the less important, then
  • 4..5 are evaluated in left to right order, then
  • 6..7 are evaluated in left to right order, then
  • 8..9 are evaluated in right to left order, then
  • A..F are evaluated in right to left order and are the most important
硬不硬你别怂 2024-09-06 07:23:29

除非您包含 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文