mysql随机排序

2020-02-13 13:37:53

参考地址 mysql随机排序

需求:首页热门栏目需要随机显示几条信息


方案1:order by rand(),全表扫描


SELECT SQL_NO_CACHE * FROM `house_rent` order by rand() limit 10

(共 1 行, 查询花费 0.1069秒)

explain SELECT * FROM `house_rent` order by rand() limit 1

id   select_type       table          type       possible_keys   key      key_len        ref         rows                           Extra

1       SIMPLE     house_rent    ALL                NULL       NULL       NULL      NULL      1009   Using temporary; Using filesort


方案2:拆分两个sql语句,第一个先随机取得id,可以用到index索引,第二个根据这些id直接定位。总用时0.0021,单需要两次请求


SELECT SQL_NO_CACHE id FROM `house_rent` order by rand() limit 10 

(共 1 行, 查询花费 0.0012 秒)

explain SELECT id FROM `house_rent` order by rand() limit 10 

id   select_type       table          type       possible_keys   key      key_len        ref         rows                           Extra 

1       SIMPLE     house_rent    index             NULL             uid             5             NULL     1690    Using index; Using temporary; Using filesort


SELECT SQL_NO_CACHE * FROM `house_rent` where id in (298,106,533,545,2,446,1367,1509,759,499)

(共 10 行, 查询花费 0.0009 秒)


方案3:将方案2两次请求用子查询合并为一次,查询时间比方案2高5倍,可见效果并不好,为了满足mysql语法生成了2张临时表。但是这种子查询并没有引发了笛卡尔积


SELECT SQL_NO_CACHE * FROM `house_rent` where id in ( select id from (SELECT id FROM `house_rent` order by rand() limit 10 ) as a )

(共 10 行, 查询花费 0.0110 秒)

SELECT SQL_NO_CACHE * FROM `house_rent` where id in ( select id from (SELECT id FROM `house_rent` order by rand() limit 50 ) as a )

(共 50 行, 查询花费 0.0071 秒)


方案4:利用子查询先计算一个随机点,然后再取小于该随机点的n条数据,因为取值是连贯的,所以速度比较快。而且子查询语句是纯数学计算,也不会有临时表影响。此方案虽然结果是连续的几个值,但每次获取的内容不一样,可以满足需求


select SQL_NO_CACHE * from house_rent where status=1 and cid=4 and (select ceil(rand()*(select MAX( id ) from house_rent)))<=id limit 10

(共 10 行, 查询花费 0.0011 秒)




explain select SQL_NO_CACHE * from house_rent where status=1 and cid=4 and (select ceil(rand()*(select MAX( id ) from house_rent)))<=id limit 10

id   select_type       table                 type       possible_keys   key      key_len        ref         rows                           Extra 

1      PRIMARY       house_rent       ref            status             status          2             const     1478                Using where

3     SUBQUERY        NULL          NULL         NULL                NULL       NULL       NULL     NULL     Select tables optimized away


  • 2019-01-29 14:33:07

    child_process中spawn和exec方法的使用

    child_process是nw.exe的一个内置模块,通过它可以实现创建多线程,并可实现主线程和子线程之间的通信。child_process模块中主要使用有两个方法spawn和exec,这两个方法都可以用来创建子线程。除了spawn和exec外,child_process模块还有execFile,fork,spawnSync,execFileSync,execSync,它们都是基于spawn的不同封装。 --------------------- 作者:黄泽平 来源:CSDN 原文:https://blog.csdn.net/zeping891103/article/details/52230175 版权声明:本文为博主原创文章,转载请附上博文链接!

  • 2019-01-29 14:50:51

    Node.js 编写跨平台 spawn 语句

    Node.js 是跨平台的,也就是说它能运行在 Windows、OSX 和 Linux 平台上。很多 Node.js 开发者都是在 OSX 上做开发的,然后再将代码部署到 Linux 服务器上。由于 OSX 和 Linux 都是基于 Unix 的,因此两者共性很多。Windows 也是 Node.js 官方支持的平台,只要你通过正确的方式写代码,就能在各个平台上毫无压力的跑起来。

  • 2019-01-30 17:53:21

    视图与临时表

    视图与表的不同之处:视图是一个虚表,即视图所对应的数据不进行实际存储,数据库只存储视图的定义,对视图的数据进行操作时,系统根据视图的定义去操作与视图相关联的基本表。

  • 2019-02-01 08:43:59

    JS 随机排序算法

    使用JS编写一个方法 让数组中的元素每次刷新随机排列