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 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 第一种速度比第二种快的多…
最近在做接口测试的时候,测试数据是从数据库查询的,但是当需要并发多次去调用接口时,如果sql只是单纯的进行了limit取值,那并发的时候肯定会每条数据都一样. 因此,研究了下sql随机取一条数据的写法,从而用大量不同数据去进行并发的接口性能测试 原文:https://www.cnblogs.com/my_life/articles/7827233.html 三种方法: (1)ORDER BY RAND() 写法:SELECT * FROM 表A ORDER BY RAND() LIMIT 1 缺…