Is it correct to scan a table in MySQL using “SELECT * .. LiMIT start, count” without an ORDER BY clause?

我怕爱的太早我们不能终老 提交于 2020-01-16 19:00:11

问题


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).


回答1:


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



回答2:


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!



来源:https://stackoverflow.com/questions/4374035/is-it-correct-to-scan-a-table-in-mysql-using-select-limit-start-count-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!