数据库如何实现SQL“ORDER BY x”?

发布于 2024-11-28 22:34:37 字数 146 浏览 0 评论 0原文

我想知道这些在幕后是如何工作的,特别是对于大型结果集。例如,数据库是否可能将原始结果写入磁盘,然后进行外部排序?

我还想知道这如何与 LIMIT...OFFSET 一起使用。如果数据库无法利用现有索引,那么数据库似乎必须对整个事物进行排序并提取原始结果集的子集。

I'm wondering how these work under the hood, especially for large result sets. For example, would the DB likely write the raw result to disk and then do an external sort?

I'm also wondering how this works with LIMIT...OFFSET. If the DB can't utilize an existing index it seems like the DB would have to sort the whole thing and pluck the subset of the raw result set.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

差↓一点笑了 2024-12-05 22:34:37

索引是有序的;如果有合适的索引,就会使用它。否则,他们将需要进行排序,正如您所想的那样。查询的执行计划(您可以使用例如 EXPLAIN 或通过客户端菜单获取它的具体方法因情况而异;这DBMS)可能包含有关如何对查询进行排序的提示。

请参阅:

  1. MySQL:ORDER BY 优化
  2. PostgreSQL:索引和 ORDER BY
  3. SQL Server 索引
  4. Oracle:了解索引

Indexes are ordered; if there's a suitable index, that will be used. Otherwise, they'll need to sort, as you suppose. The execution plan for a query (which you can get with e.g. EXPLAIN or via client menus; the exact method of getting it varies with the DBMS) may contain hints as to how a query will be sorted.

See:

  1. MySQL: ORDER BY Optimization
  2. PostgreSQL: Indexes and ORDER BY
  3. SQL Server Indexes
  4. Oracle: Understanding Indexes
此生挚爱伱 2024-12-05 22:34:37

mySQL 在 此链接

Oracle展示了自己的Order By算法过程这里

基本上,如果你有一个索引,它已订购。但是当你不排序时,就会发生O(n log n)

mySQL shows their own Order By optimization on this link

Oracle shows theor own Order By algorithm procedure here

Basically, If you have an index, it is ordered. But when you don't sorting occurs which is O(n log n)

爱给你人给你 2024-12-05 22:34:37

你几乎有权利这样做。如果没有准备或预先计划任何内容(即索引或之前准备或缓存的数据),那么是的,必须读取为了生成正确的顺序而必须考虑的所有数据,并且如果数据量达到无法适应可用/分配的内存,则需要进行磁盘缓存。

这是一个不平凡的性能问题,每个 RDBMS 都会有巧妙的方法来解决和优化它,这样您就可以使用他们的产品,而不是那种劣质的“Brand X”山寨产品。

You pretty much have the right of it. If nothing has been prepared or pre-planned (i.e. indexes, or data otherwise previously prepared or cached), then yes, all the data that must be considered in order to generate the proper order must be read, and if the amount of data to be worked over cannot fit in available/allocated memory, then disk caching will need to be done.

It's a non-trivial performance issue, and every RDBMS will have clever ways to address and optimize it, so that you'll use their product and not that shoddy "Brand X" knock-off.

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