mysql group by如何优化和提高查询速度?

时间:2018-09-25 09:28:52

标签: php mysql

SELECT `time`,count(uid) as log 
FROM ( 
      SELECT distinct uid,
      FROM_UNIXTIME(createtime,"%Y-%m-%d") as time FROM `yq_user_log` 
      WHERE `createtime` BETWEEN 1535126400 AND 1537891199 AND `type` = 'login'  
     ) a GROUP BY time 

1 个答案:

答案 0 :(得分:1)

对于初学者来说,您甚至不需要子查询:

SELECT
    FROM_UNIXTIME(createtime, '%Y-%m-%d') AS time,
    COUNT(DISTINCT uid) AS log
FROM yq_user_log
WHERE createtime BETWEEN 1535126400 AND 1537891199 AND type = 'login'
GROUP BY time;

我不确定这里是否有任何索引会有所帮助,因为您是按列的功能分组的。这将排除索引对GROUP BY的帮助。