任何人都可以帮助我提高这个查询的效率,有时它最多需要 3 秒才能执行:
SELECT
up.*,
u.name AS learner_name,
g.type AS group_type,
l.name AS name,
l.type AS type,
l.assessment_type AS assessment_type,
l.id AS lesson_id,
s.sort AS sort
FROM
cdu_user_progress up
INNER JOIN cdu_groups g ON g.id = up.group_id
INNER JOIN cdu_sessions s ON s.id = up.session_id
INNER JOIN cdu_lessons l ON l.id = up.lesson_id
INNER JOIN users u ON u.uid = up.uid
INNER JOIN field_data_field_teacher_id ftid ON ftid.entity_id = up.uid
WHERE
(ftid.field_teacher_id_value = '6378')
ORDER BY
up.id DESC
LIMIT 15
OFFSET 0
cdu_user_progress (up) 是 BIG 表,它包含 >200k 行。其他表只包含几百行。
解释返回这个:
id |select_type |table |type |possible_keys |key |key_len |ref |rows |Extra
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 |SIMPLE |l |ALL |PRIMARY |NULL |NULL |NULL |744 |Using temporary; Using filesort
1 |SIMPLE |up |ref |cdu_user_progress(uid, lesson_id, level, |cdu_user_progress(lesson_id, game_id) |4 |ealteach_main.l.id |133 |
| | | |game_id, date),cdu_user_progress(uid, | | | | |
| | | |lesson_id, level, game_id, score), | | | | |
| | | |cdu_user_progress(lesson_id, game_id), | | | | |
| | | |idx_score,idx_id_update | | | | |
1 |SIMPLE |g |eq_ref |PRIMARY |PRIMARY |4 |ealteach_main.up.group_id |1 |
1 |SIMPLE |ftid |ref |entity_id |entity_id |4 |ealteach_main.up.uid |1 |Using index condition; Using where
1 |SIMPLE |u |eq_ref |PRIMARY |PRIMARY |4 |ealteach_main.up.uid |1 |Using where
1 |SIMPLE |s |eq_ref |PRIMARY |PRIMARY |4 |ealteach_main.up.session_id |1 |
不确定我是否读对了,但它似乎正在扫描整个 cdu_lessons (l) 表 - 这是瓶颈所在吗?
希望有人能帮忙!
最佳答案
当您执行 INNER JOINS
时,请确保您首先包含来自其他表的列,然后是连接表的列。这会将您的查询优化 10-20%,这对于其他查询也是一种很好的做法,例如:
INNER JOIN cdu_groups g ON up.group_id = g.id
INNER JOIN cdu_sessions s ON up.session_id = s.id
其他表依此类推..
关于MySql multiple join耗时长(只有一个表很大),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34655376/