学新通技术网

mysql 随机RAND和GROUP BY优化

Luke 13871 1
mysql 随机RAND和GROUP BY优化

group by 优化

SELECT goods_id,count(*) FROM t GROUP BY goods_id;

默认情况下,MySQL对所有GROUP BY col1,col2…的字段进行排序。如果查询包括GROUP BY,想要避免排序结果的消耗,则可以指定ORDER BY NULL禁止排序。

修改成

SELECT goods_id,count(*) FROM t GROUP BY goods_id ORDER BY NULL;

RAND随机优化

SELECT * FROM t1 WHERE 1=1 ORDER BY RAND() LIMIT 4;

MySQL不支持函数索引,会导致全表扫描

修改成

SELECT * FROM t1 WHERE id >= CEIL(RAND()*1000) LIMIT 4;

 

本文出至:学新通技术网

标签: