在项目中,我检测到一个慢查询。有时它持续超过30秒。最有可能的是,由于它的连接,它很慢。它是一个学习网站的网站,查询显示学习词汇表的结果。 首先,我必须要求用户的词汇表。随后我询问了列表中的条款,最后,我必须找出这些条款的结果。
lists:
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| ul_id | int(11) | NO | PRI | NULL | auto_increment |
| ul_user | int(11) | NO | MUL | NULL | |
| […]
+----------+--------------+------+-----+---------+----------------+
Index on ul_user
key table lists/vocs
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| rlv_list | int(11) | NO | PRI | NULL | |
| rlv_voc | int(11) | NO | PRI | NULL | |
+----------+---------+------+-----+---------+-------+
Indices both columns
results:
+-------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| rv_user | int(11) | YES | MUL | NULL | |
| rv_voc | int(11) | YES | MUL | NULL | |
| rv_result | int(11) | YES | | NULL | |
| […]
+-------------+---------+------+-----+---------+-------+
Index on rv_user and rv_voc
这是我的疑问:
SELECT COUNT(*) AS total,
SUM(rv_result = 1) AS correct,
SUM(rv_result = 2) AS incorrect
FROM user_lists
LEFT JOIN rel_lists_vocs ON rlv_list = ul_id
LEFT JOIN results_vocabulary ON rv_voc = rlv_voc
WHERE ul_user = :user AND rv_user = :user
GROUP BY ul_id
如何更快地进行此查询?提前谢谢。
编辑:解释结果:
+----+-------------+--------------------+------+-------------------------------+--------------+---------+---------------------------------------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------------+------+-------------------------------+--------------+---------+---------------------------------------+------+--------------------------+
| 1 | SIMPLE | lists | ref | PRIMARY,ul_user | ul_user | 4 | const | 18 | Using where; Using index |
| 1 | SIMPLE | lists_vocs | ref | noduplicates,rlv_list,rlv_voc | noduplicates | 4 | xx.ul_id | 14 | Using where; Using index |
| 1 | SIMPLE | results | ref | rv_user,rv_voc | rv_voc | 5 | xx.rlv_voc | 67 | Using where |
+----+-------------+--------------------+------+-------------------------------+--------------+---------+---------------------------------------+------+--------------------------+