我需要优化这个查询:优化非常缓慢的MySQL查询
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
)
EXPLAIN输出:
+----+--------------------+-------+------+-----------------+-------+---------+-----------------------------------------+--------+-------------+
| 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计划。你有什么指标在桌子上? – 2012-01-12 14:36:59
我想建议你将查询分成两部分。原因:易于维护,易于缓存,可能执行起来更快 – Anatoly 2012-01-12 14:50:22
表是MyISAM还是InnoDB? – 2012-04-29 15:38:54