mysql orderby+分页 乱序原因和解决

MySQL在使用LIMIT和ORDERBY时可能不按预期排序,尤其当orderby字段值相同时。这源于MySQL的查询优化策略,可能导致非确定性结果。解决方法是添加其他列到ORDERBY以确保顺序。
摘要由CSDN通过智能技术生成

在 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 no HAVING 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*** with ORDER BY, MySQL stops sorting as soon as it has found the first row_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 the LIMIT clause are selected, and most or all of them are sorted, before the first ro
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值