简体   繁体   中英

mysql group by How to optimize and improve query speed?

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 

For starters, you don't even need to subquery:

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;

I'm not sure that any index would help much here, because you are grouping by a function of a column. This would preclude an index helping with GROUP BY .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM