对 order by random() 的理解
开始
order by random() ,相当于不是根据某个字段来排序,而是乱序。这个方法可以帮助制造混乱分布的测试数据。
postgres=# \d employee Table "public.employee" Column | Type | Modifiers --------+-----------------------+----------- id | integer | name | character varying(20) | deptno | integer | age | integer | Indexes: "idx_id_dept" btree (id, deptno) postgres=# select name, age from employee order by name limit 5; name | age -------+----- gao | 30 jian | 35 nam04 | 25 nam05 | 40 nam06 | 32 (5 rows) postgres=# select name, age from employee order by age limit 5; name | age -------+----- nam10 | 25 nam16 | 25 nam04 | 25 nam28 | 25 nam22 | 25 (5 rows) postgres=# select name, age from employee order by random() limit 5; name | age --------+----- nam560 | 40 nam758 | 40 nam635 | 40 nam307 | 25 nam708 | 30 (5 rows) postgres=# select name, age from employee order by random() limit 5; name | age --------+----- nam490 | 25 nam444 | 32 nam943 | 25 nam143 | 35 nam690 | 32 (5 rows) postgres=# select name, age from employee order by 0.1 limit 5; ERROR: non-integer constant in ORDER BY LINE 1: select name, age from employee order by 0.1 limit 5; ^ postgres=# select name, age from employee order by random() limit 5; name | age --------+----- nam214 | 25 nam474 | 32 nam175 | 25 nam22 | 25 nam210 | 32 (5 rows) postgres=# select name, age from employee order by random() limit 5; name | age --------+----- nam390 | 30 nam626 | 35 nam342 | 30 nam620 | 35 nam674 | 35 (5 rows) postgres=# select name, age from employee order by random() limit 5; name | age --------+----- nam242 | 35 nam847 | 30 nam509 | 35 nam945 | 32 nam721 | 25 (5 rows) postgres=# select name, age from employee order by random() limit 5; name | age --------+----- nam165 | 32 nam956 | 40 nam446 | 35 nam65 | 40 nam291 | 30 (5 rows) postgres=#
[作者:技术者高健@博客园 mail: luckyjackgao@gmail.com ]
结束