Wednesday, September 8, 2010

MySQL性能优化21 - 使用随机函数产生采样

RAND() 函数是 MySQL 提供的产生随机数的函数。没有指定参数时,每次执行会返回 0-1 的浮点数:

[cc lang="text"]mysql> select rand();

+------------------+
| rand() |
+------------------+
| 0.56110724207117 |
+------------------+

1 row in set (0.02 sec)

mysql> select rand();

+-----------------+
| rand() |
+-----------------+
| 0.3343874880433 |
+-----------------+

1 row in set (0.00 sec)[/cc]
有些时候,我们需要在统计操作的时候进行随机的采样,可以使用 ORDER BY RAND() 按照随机顺序检索数据行,如下:
[cc lang="text"]
mysql> select * from employee order by rand();

+------+--------+------+------+------+
| id | name | dep | pos | sal |
+------+--------+------+------+------+
| 4 | cathey | 02 | 02 | 3000 |
| 10 | sun | 02 | 02 | 1900 |
| 6 | lily | 03 | 02 | 2200 |
| 3 | micro | 02 | 01 | 1500 |
......[/cc]
ORDER BY RAND() 同 LIMIT 的结合可以从一组列中选择随机样本,下面的例子就是使用 ORDER BY RAND() LIMIT 从雇员表随机获得 6 个样本的方法:
[cc lang="text"]
mysql> select * from employee order by rand() limit 6;

+------+--------+------+------+------+
| id | name | dep | pos | sal |
+------+--------+------+------+------+
| 4 | cathey | 02 | 02 | 3000 |
| 5 | amy | 03 | 01 | 2500 |
| 2 | eefs | 01 | 02 | 2000 |
| 3 | micro | 02 | 01 | 1500 |
| 10 | sun | 02 | 02 | 1900 |
| 1 | abcd | 01 | 01 | 1000 |
+------+--------+------+------+------+

6 rows in set (0.00 sec)[/cc]
随机抽取样本对总体的统计具有十分重要的意义,这个函数在进行统计操作的时候非常有用。

1 comment: