使用“SELECT * .. LiMIT start, count”扫描 MySQL 中的表是否正确?没有 ORDER BY 子句?

发布于 2024-10-06 08:58:53 字数 549 浏览 1 评论 0原文

假设表 X 有 100 个元组。

以下扫描 X 的方法会在 MySQL 中生成表 X 中的所有元组吗?

for start in [0, 10, 20, ..., 90]:
    print results of "select * from X LIMIT start, 10;"

我问,因为我一直在使用 PostgreSQL,它清楚地表明这种方法 不需要工作,但似乎没有这样的信息 MySQL。如果不会,是否有一种方法可以在不知道有关表的任何其他信息(例如主键字段是什么)的情况下以固定顺序返回结果?

我需要扫描应用程序中表中的每个元组,并且我希望有一种方法可以在不使用应用程序中太多内存的情况下执行此操作(因此只需执行“select * from X”即可)。

Suppose Table X has a 100 tuples.

Will the following approach to scanning X generate all the tuples in TABLE X, in MySQL?

for start in [0, 10, 20, ..., 90]:
    print results of "select * from X LIMIT start, 10;"

I ask, because I've been using PostgreSQL, which clearly says that this approach need not work, but there seems to be no such info for MySQL. If it won't, is there a way to return results in a fixed ordering without knowing any other info about the table (like what the primary key fields are)?

I need to scan each tuple in a table in an application, and I want a way to do it without using too much memory in the application (so simply doing a "select * from X" is out).

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

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

发布评论

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

评论(2

爱的十字路口 2024-10-13 08:58:53

不,这不是一个安全的假设。如果没有 ORDER BY 子句,则无法保证您的查询每次都会返回唯一的结果。如果该表已正确建立索引,则添加 ORDER BY (对于索引)应该不会太昂贵。

编辑:非 ORDER BY 编辑的结果有时会按照聚集索引的顺序排列,但我不会为此投入任何金钱!

No, that isn't a safe assumption. Without an ORDER BY clause, there is no guaranteeing that your query will return unique results each time. If this table is properly indexed, adding an ORDER BY (for the index) shouldn't be too expensive.

Edit: Non-ORDER BYed results will sometimes be in the order of the clustered index, but I wouldn't put any money on that!

情独悲 2024-10-13 08:58:53

如果您使用 Innodb 或 MyISAM 表类型,更好的方法是使用 HANDLER 接口。只有 MySQL 支持此功能,但它可以满足您的要求:

http://dev .mysql.com/doc/refman/5.0/en/handler.html

另外,MySQL API 支持两种从服务器检索数据的模式:

  1. 存储结果:在这种模式下,一旦执行查询, API 在返回用户代码之前检索整个结果集。这会消耗大量客户端内存缓冲结果,但最大限度地减少服务器上资源的使用。
  2. 使用结果:在此模式下,API 逐行提取结果并将控制权更频繁地返回给用户代码。这最大限度地减少了客户端上的内存使用,但可以在服务器上保持更长时间的锁定。

大多数不同语言的 MySQL API 都以某种形式支持这一点。它通常是创建连接时提供的参数,和/或可用于现有连接以将其切换到该模式的单独调用。

因此,为了回答你的问题 - 我会执行以下操作:

set the connection to "use result" mode;
select * from X

If you are using Innodb or MyISAM table types, a better approach is to use the HANDLER interface. Only MySQL supports this, but it does what you want:

http://dev.mysql.com/doc/refman/5.0/en/handler.html

Also, the MySQL API supports two modes of retrieving data from the server:

  1. store result: in this mode, as soon as a query is executed, the API retrieves the entire result set before returning to the user code. This can use up a lot of client memory buffering results, but minimises the use of resources on the server.
  2. use result: in this mode, the API pulls results row-by-row and returns control to the user code more frequently. This minimises the use of memory on the client, but can hold locks on the server for longer.

Most of the MySQL APIs for various languages support this in oneform or another. It is usually an argument that can be supplied as when creating the connection, and / or a separate call that can be used against an existing connection to switch it to that mode.

So, in answer to your question - I would do the following:

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