【发布时间】:2012-02-08 20:09:21
【问题描述】:
我需要帮助优化此查询:
SELECT
c.rut, c.nombre, c.apellido, c.estado, c.porcentajeavance,
c.porcentajenota, c.nota, c.curso, c.fecha_inicio,
c.fecha_termino, c.grupo, c.fecha, c.cargo
FROM tbl_historico c
WHERE fecha = (
SELECT max( t.fecha ) fecha
FROM tbl_historico t
WHERE t.rut = c.rut AND c.curso = t.curso
)
解释输出:
+----+--------------------+-------+------+-----------------+-------+---------+-----------------------------------------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+------+-----------------+-------+---------+-----------------------------------------+--------+-------------+
| 1 | PRIMARY | c | ALL | NULL | NULL | NULL | NULL | 158008 | Using where |
| 2 | DEPENDENT SUBQUERY | t | ref | rut,rut_2,rut_3 | rut_3 | 514 | campus_mdle1.c.rut,campus_mdle1.c.curso | 27 | Using index |
+----+--------------------+-------+------+-----------------+-------+---------+-----------------------------------------+--------+-------------+
【问题讨论】:
-
在问题中添加 EXPLAIN 计划。你有什么索引?
-
我建议您将查询一分为二。原因:易于维护、易于缓存并且执行速度可能更快
-
表是 MyISAM 还是 InnoDB ?
标签: mysql sql subquery query-tuning