php MySQL 选择随机行 - rand() 性能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16289287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:51:42  来源:igfitidea点击:

MySQL select random row - rand() performance

phpmysql

提问by Biker John

Is it true that ORDER BY rand()performance is very slow compared to other solutions? If yes, what are better ways to select random row(s) from the database?

ORDER BY rand()与其他解决方案相比,性能真的很慢吗?如果是,从数据库中选择随机行的更好方法是什么?

My query:

我的查询:

SELECT sName FROM bpoint WHERE placeID=? ORDER BY rand() LIMIT 1; 

采纳答案by bestprogrammerintheworld

Yes, ORDER BY RAND()can be very slow in larger result-sets.

是的,ORDER BY RAND()在较大的结果集中可能会非常慢。

An option is to fetch resultset with this statement (into an array):

一个选项是使用此语句获取结果集(到数组中):

SELECT sName FROM bpoint WHERE placeID=?; 

After that - use array_rand($resultset)to get a randomized item from the $resultsetquery.

之后 - 用于array_rand($resultset)$resultset查询中获取随机项目。