赞
踩
在 orderby 的字段具有相同的值的时候,使用orderby + 分页可能造成乱序. 而且,相同的查询条件,是否使用limit 返回的查询结果也可能不一样。
select * from xx order by create_time desc limit 0,10
官网对此情况的解释
MySQL sometimes optimizes a query that has a
LIMIT ***row_count***
clause and noHAVING
clause:
- If you select only a few rows with
LIMIT
, MySQL uses indexes in some cases when normally it would prefer to do a full table scan.- If you combine
LIMIT ***row_count***
withORDER BY
, MySQL stops sorting as soon as it has found the firstrow_count
rows of the sorted result, rather than sorting the entire result. If ordering is done by using an index, this is very fast. If a filesort must be done, all rows that match the query without theLIMIT
clause are selected, and most or all of them are sorted, before the firstrow_count
are found. After the initial rows have been found, MySQL does not sort any remainder of the result set.
如果将 LIMIT row_count 与 ORDER BY 结合使用,MySQL 在找到排序结果的前 row_count 行后立即停止排序,而不是对整个结果进行排序。
一开始认为,如果orderby 在遇到相同值,会按照在磁盘中的顺序,或主键索引的顺序返回。
但是在查询处理过程中,mysql会根据执行计划和优化器的决策来选择执行操作的顺序和方法,包括使用不同的连接方法(如HASH连接、Nested Loop连接等)或使用不同的算法来处理查询和排序,所以导致返回顺序不一样
在 orderby 子句中包含其他列以使顺序具有确定性。如id 等
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。