¿Por qué MAX () es 100 veces más lento que ORDER BY ... LIMIT 1?
Tengo una mesafoo
con (entre otras 20) columnasbar
, baz
yquux
con índices enbaz
yquux
. La tabla tiene ~ 500k filas.
¿Por qué las siguientes preguntas difieren tanto en velocidad? La consulta A toma 0.3s, mientras que la consulta B toma 28s.
Consulta A
select baz from foo
where bar = :bar
and quux = (select quux from foo where bar = :bar order by quux desc limit 1)
Explique
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY foo ref quuxIdx quuxIdx 9 const 2 "Using where"
2 SUBQUERY foo index NULL quuxIdx 9 NULL 1 "Using where"
Consulta b
select baz from foo
where bar = :bar
and quux = (select MAX(quux) from foo where bar = :bar)
Explique
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY foo ref quuxIdx quuxIdx 9 const 2 "Using where"
2 SUBQUERY foo ALL NULL NULL NULL NULL 448060 "Using where"
Yo uso MySQL 5.1.34.