Dlaczego MAX () 100 razy wolniej niż ORDER BY… LIMIT 1?
Mam stolikfoo
z (wśród 20 innych) kolumnbar
, baz
iquux
z indeksami nabaz
iquux
. Tabela ma ~ 500 tys. Rzędów.
Dlaczego następujące kwestie dotyczące zapytań różnią się tak bardzo szybkością? Zapytanie A zajmuje 0,3, podczas gdy zapytanie B zajmuje 28 sekund.
Zapytanie A
select baz from foo
where bar = :bar
and quux = (select quux from foo where bar = :bar order by quux desc limit 1)
Wyjaśniać
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"
Zapytanie B
select baz from foo
where bar = :bar
and quux = (select MAX(quux) from foo where bar = :bar)
Wyjaśniać
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"
Używam MySQL 5.1.34.