如何加快mysql查询的执行速度?

时间:2019-07-18 14:37:16

标签: mysql sql

output of this query 我的查询有点复杂,大约需要3分钟才能提取100条记录。我如何调整查询以加快其执行速度?我正在使用mysql。

我已经在可能的字段上添加了索引。

EXPLAIN SELECT RESULT.*,IFNULL(RESULT1.AVG_WEEK_TIME_SPENT,0) AVG_WEEK_TIME_SPENT FROM (
    SELECT I.ID,I.CLIENT_INST_ID,I.NAME,U2.USERNAME INSTITUTE_EMAIL,
    IFNULL(SUM(TD.TIME_SPENT),0) TOTAL_TIME_SPENT,COUNT(DISTINCT U.ID) REGISTERED_USER,
    COUNT(DISTINCT TD.user_id) ACTIVE_USER,
    IFNULL(IFNULL(SUM(TD.TIME_SPENT),0)/COUNT(DISTINCT TD.user_id),0) AVG_TIME_SPENT
    FROM cloudCore.USER U,cloudCore.USER U2,client219.INSTITUTE I,client219.INSTITUTE_USER_MAP IUM
    LEFT OUTER JOIN client219.LICENSE_CONSUMPTION LC ON LC.user_id = IUM.ID AND LC.CONSUMPTION <='2019-02-28 23:59:59'
    LEFT OUTER JOIN client219.USER_BOOK UB ON UB.institute_user_id =LC.user_id AND (UB.EXPIRY_DATE >='2018-02-01 00:00:00' OR UB.EXPIRY_DATE IS NULL)
    LEFT OUTER JOIN analytics.TRACKING_DATA TD ON UB.institute_user_id=TD.user_id AND TD.INSTITUTE_ID=IUM.institute_id AND  TD.client_id=219
    AND TD.LAST_ACCESSED >='2018-02-01 00:00:00' AND TD.LAST_ACCESSED <='2019-02-28 23:59:59'
    WHERE U.client_id=219 AND U.LEVEL=2 AND IUM.user_id=U.ID AND U.DELETED = 0
    AND IUM.institute_id = I.ID AND U2.id=I.ADMIN_ID AND U2.client_id=U.client_id
    GROUP BY I.ID ) RESULT
    LEFT OUTER JOIN
 (SELECT I.ID,IFNULL(IFNULL(SUM(TD.TIME_SPENT),0)/COUNT(DISTINCT TD.user_id),0) AVG_WEEK_TIME_SPENT
FROM cloudCore.USER U,client219.INSTITUTE I,client219.INSTITUTE_USER_MAP IUM
LEFT OUTER JOIN client219.LICENSE_CONSUMPTION LC ON LC.user_id = IUM.ID AND LC.CONSUMPTION <='2019-02-28 23:59:59'
LEFT OUTER JOIN client219.USER_BOOK UB ON UB.institute_user_id =LC.user_id AND (UB.EXPIRY_DATE >='2018-02-01 00:00:00' OR UB.EXPIRY_DATE IS NULL)
LEFT OUTER JOIN analytics.TRACKING_DATA TD ON UB.institute_user_id=TD.user_id AND TD.INSTITUTE_ID=IUM.institute_id AND  TD.client_id=219
AND TD.LAST_ACCESSED>=DATE_ADD('2019-02-28 23:59:59',INTERVAL -7 DAY)  AND TD.LAST_ACCESSED <='2019-02-28 23:59:59'  
WHERE U.client_id=219 AND U.LEVEL=2 AND IUM.user_id=U.ID AND U.DELETED = 0
AND IUM.institute_id = I.ID AND U.CREATED_BY>=DATE_ADD('2019-02-28 23:59:59',INTERVAL -7 DAY)
GROUP BY I.ID) RESULT1 ON RESULT.ID=RESULT1.ID;

0 个答案:

没有答案