How does MySQL's ORDER BY RAND() work?

后端 未结 4 1525
渐次进展
渐次进展 2020-11-29 03:02

I\'ve been doing some research and testing on how to do fast random selection in MySQL. In the process I\'ve faced some unexpected results and now I am not fully sure I know

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 03:40

    It may have to do with indexing. id is indexed and quick to access, whereas adding username to the result, means it needs to read that from each row and put it in the memory table. With the * it also has to read everything into memory, but it doesn't need to jump around the data file, meaning there's no time lost seeking. This makes a difference only if there are variable length columns, which means it has to check the length, then skip that length, as opposed to just skipping a set length (or 0) between each row

    Practice is better that all theories! Why not just to check plans? :)

    mysql> explain select name from avatar order by RAND() limit 1;
    +----+-------------+--------+-------+---------------+-----------------+---------+------+-------+----------------------------------------------+
    | id | select_type | table  | type  | possible_keys | key             | key_len | ref  | rows  | Extra                                        |
    +----+-------------+--------+-------+---------------+-----------------+---------+------+-------+----------------------------------------------+
    |  1 | SIMPLE      | avatar | index | NULL          | IDX_AVATAR_NAME | 302     | NULL | 30062 | Using index; Using temporary; Using filesort |
    +----+-------------+--------+-------+---------------+-----------------+---------+------+-------+----------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> explain select * from avatar order by RAND() limit 1;
    +----+-------------+--------+------+---------------+------+---------+------+-------+---------------------------------+
    | id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows  | Extra                           |
    +----+-------------+--------+------+---------------+------+---------+------+-------+---------------------------------+
    |  1 | SIMPLE      | avatar | ALL  | NULL          | NULL | NULL    | NULL | 30062 | Using temporary; Using filesort |
    +----+-------------+--------+------+---------------+------+---------+------+-------+---------------------------------+
    1 row in set (0.00 sec)
    
     mysql> explain select name, experience from avatar order by RAND() limit 1;
    +----+-------------+--------+------+--------------+------+---------+------+-------+---------------------------------+
    | id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows  | Extra                           |
    +----+-------------+--------+------+---------------+------+---------+------+-------+---------------------------------+
    |  1 | SIMPLE      | avatar | ALL  | NULL          | NULL | NULL    | NULL | 30064 | Using temporary; Using filesort |
    +----+-------------+--------+------+---------------+------+---------+------+-------+---------------------------------+
    

提交回复
热议问题