mysql div 没有小数,mysql order by limit 的一个坑

mysql order by limit 的一个坑

分页查询的时候遇到的坑:

发现的问题: 对单个无索引的字段进行排序后limit .发现当被排序字段有相同值时并且在limit范围内,取的值并不是正常排序后的值,

也就是说,当排在第N行的数据可取key1、 key2 时 , 排序结果可能是key1,也可能是key2。

4e33d86d68cb6a1c11af780f43a06f16.png

排序+ limit 结果 (排序键无索引)

按cnt取key_word分别前三结果:

df9e7d2b222196e89083a92df6bf7e9d.png

解决: 先上个链接!

https://dev.mysql.com/doc/refman/5.7/en/limit-optimization.html

这是mysql对limit的优化。

原文:

If multiple rows have identical values in the ORDER BY columns, the server is free to return those rows in any order, and may do so differently depending on the overall execution plan. In other words, the sort order of those rows is nondeterministic with respect to the nonordered columns.

是说如果order by的列有相同的值时, mysql会随机选取这些行,具体根据执行计划有所不同。

解决: order by 的列中包含一个索引列

此处增加主键id为排序列

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值