商城首页欢迎来到中国正版软件门户

您的位置:首页 >mysql order by limit的坑怎么解决

mysql order by limit的坑怎么解决

  发布于2023-04-27 阅读(0)

扫一扫,手机访问

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

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

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

mysql order by limit的坑怎么解决

排序+ limit 结果 (排序键无索引) 
按cnt取key_word分别前三结果:

mysql order by 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为排序列

本文转载于:https://www.yisu.com/zixun/637164.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注