SELECT * FROM tablename AS r1 JOIN (SELECT ROUND(RAND() *(SELECT MAX(id)FROM tablename)) AS id) AS r2 WHERE r1.id >= r2.id ORDER BY r1.id ASC LIMIT 10000; SELECT * FROM tablename ORDER BY rand() LIMIT 5 第一种速度比第二种快的多…
SELECT * FROM tableName ORDER BY RAND() LIMIT n 数据量小的话还可以, 数据量大起来了, 就影响性能了. $rubbish = (new \yii\db\Query()) ->select('r.name as rubbish,r.id as rid, c.id, c.name, c.code, c.inc, c.des,c.req') ->from('recycle_rubbishkeytocategory AS r') ->…
1)使用join 和 rand() 耗时 0.009 SELECT * FROM `t_topic` AS t1 JOIN ( SELECT ROUND( RAND() * ( (SELECT MAX(id) FROM `t_topic`) - (SELECT MIN(id) FROM `t_topic`) ) + (SELECT MIN(id) FROM `t_topic`) ) AS id ) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id LIMIT ;…
暂时记录下: SELECT * FROM `kw_user` AS t1 JOIN ( SELECT ROUND( RAND( ) * ( ( SELECT MAX( id ) FROM `kw_user` ) - ( SELECT MIN( id ) FROM `kw_user` ) ) + ( SELECT MIN( id ) FROM `kw_user` ) ) AS id ) AS t2 WHERE t1.id >= t2.id AND 加上删选条件 ORDER BY t1.…
--从MySQL随机选取数据 -------------------------2014/06/23 从MySQL随机选取数据最简单的办法就是使用”ORDER BY RAND()”; 方案一: SELECT * FROM `table` ORDER BY RAND() LIMIT 0,1; 这种方法的问题就是非常慢.原因是因为MySQL会创建一张零时表来保存所有的结果集,然后给每个结果一个随机索引,然后再排序并返回. 有几个方法可以让它快起来. 基本思想就是先获取一个随机数,然后使用这个随机数来…
最近在做接口测试的时候,测试数据是从数据库查询的,但是当需要并发多次去调用接口时,如果sql只是单纯的进行了limit取值,那并发的时候肯定会每条数据都一样. 因此,研究了下sql随机取一条数据的写法,从而用大量不同数据去进行并发的接口性能测试 原文:https://www.cnblogs.com/my_life/articles/7827233.html 三种方法: (1)ORDER BY RAND() 写法:SELECT * FROM 表A ORDER BY RAND() LIMIT 1 缺…