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


  • 2020-12-17 10:55:48

    html5 video p2p research

    节约带宽,减少缓冲时间,提升服务质量,处理峰值流量, 视频观看的人越多,播放越流畅。

  • 2020-12-17 10:57:34

    使用 MediaSource 搭建流式播放器

    Media Source Extensions(媒体源扩展)大大地扩展了浏览器的媒体播放功能,提供允许JavaScript 生成媒体流。这可以用于自适应流(adaptive streaming,也是我毕设的研究方向)及随时间变化的视频直播流(live streaming)等应用场景。

  • 2020-12-17 11:00:37

    H5流式播放(FMP4转封装与mediaSource)

    W3C上有明确关于mediaSource 扩展接口的文档。mediaSource 扩展文档中是这么定义的, 它允许JS脚本动态构建媒体流用于和,允许JS传送媒体块到H5媒体元素。这种接口的应用可以让h5播放器实现持续添加数据进行播放。做as的朋友都知道as中的appendBytes方法,一种添加二进制数据进行播放的方式。这两种接口在概念上是类似的。只是里面的定义和对媒体文件的要求有所不同。对于mediaSource扩展接口我只介绍我们主要应用的几个。

  • 2020-12-18 17:15:29

    coTurn stun服务器搭建,禁用turn

    https://github.com/coturn/coturn 在这里git clone 下来然后编译安装,一切默认即可。编译后,也可以不用安装。在编译目录下bin文件夹下有turnserver turnutils_stunclient turnutils_uclient 这三个等一下会用到的软件。

  • 2020-12-18 17:26:25

    coturn配置文件详细解释

    Coturn 是webrtc,p2p视频通话必不少的,主要包含2个主要功能stun服务, turn服务 Coturn 的githup地址为 https://github.com/coturn/coturn/

  • 2020-12-21 06:26:16

    为UIView添加点击事件

    最近经常碰到要将UIImageView和UILabel看成整体的情况,我于是就将他俩用UIView包起来,那么怎么给一个UIView添加点击事件,可以这么实现: