zoukankan      html  css  js  c++  java
  • mysql order by rand() 优化方法

    mysql order by rand() 优化方法 适用于领取奖品等项目
    <pre>
    mysql> select * from user order by rand() limit 1;
    +-------+------------+----------------------------------+----------+--------------+-----------+
    | id | phone | password | salt | country_code | ip |
    +-------+------------+----------------------------------+----------+--------------+-----------+
    | 15160 | 6549721306 | e4f302120c006880a247b652ad0e42f2 | 40343586 | 86 | 127.0.0.1 |
    +-------+------------+----------------------------------+----------+--------------+-----------+
    1 row in set (0.25 sec)
    mysql> explain select * from user order by rand() limit 1;
    +----+-------------+-------+------+---------------+------+---------+------+--------+---------------------------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-------+------+---------------+------+---------+------+--------+---------------------------------+
    | 1 | SIMPLE | user | ALL | NULL | NULL | NULL | NULL | 200303 | Using temporary; Using filesort |
    +----+-------------+-------+------+---------------+------+---------+------+--------+---------------------------------+
    1 row in set (0.00 sec)
    </pre>
    根据分析结果,运行需要0.25秒,order by rand() 需要使用临时表(Using temporary),需要使用文件排序(Using filesort),效率低下。


    改进方法
    <pre>
    <pre>
    <?php
    // 获取总记录数
    $sqlstr = 'select count(*) as recount from user';
    $query = mysql_query($sqlstr) or die(mysql_error());
    $stat = mysql_fetch_assoc($query);
    $total = $stat['recount'];

    // 随机偏移
    $offset = mt_rand(0, $total-1);

    // 偏移后查询
    $sqlstr = 'select * from user limit '.$offset.',1';
    $query = mysql_query($sqlstr) or die(mysql_error());
    $result = mysql_fetch_assoc($query);

    print_r($result);
    ?>
    </pre>
    <pre>
    mysql> select * from user limit 23541,1;
    +-------+------------+----------------------------------+----------+--------------+-----------+
    | id | phone | password | salt | country_code | ip |
    +-------+------------+----------------------------------+----------+--------------+-----------+
    | 23542 | 3740507464 | c8bc1890de179538d8a49cc211859a46 | 93863419 | 86 | 127.0.0.1 |
    +-------+------------+----------------------------------+----------+--------------+-----------+
    1 row in set (0.01 sec)

    mysql> explain select * from user limit 23541,1;
    +----+-------------+-------+------+---------------+------+---------+------+--------+-------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-------+------+---------------+------+---------+------+--------+-------+
    | 1 | SIMPLE | user | ALL | NULL | NULL | NULL | NULL | 200303 | NULL |
    +----+-------------+-------+------+---------------+------+---------+------+--------+-------+
    1 row in set (0.00 sec)
    </pre>
    </pre>

  • 相关阅读:
    【spring源码学习】spring的IOC容器之自定义xml配置标签扩展namspaceHandler向IOC容器中注册bean
    【spring源码学习】spring的IOC容器在初始化bean过程
    【spring源码学习】Spring的IOC容器之BeanPostProcessor接口学习
    Zookeeper之Zookeeper的Client的分析
    Zookeeper之Zookeeper底层客户端架构实现原理(转载)
    【LIUNX】目录或文件权限,权限授予
    Java 8 日期时间API使用介绍
    Java 8中的 Streams API 详解
    Java 8 Lambda表达式介绍
    Java中Comparable和Comparator区别小结
  • 原文地址:https://www.cnblogs.com/newmiracle/p/11865478.html
Copyright © 2011-2022 走看看