我的查詢太長(3-4s)。任何想法如何使這個更快?MySQL Query - alternative
SELECT u.id AS id_uzytkownika,
u.login,
u.ranga,
u.online_light AS online,
(SELECT MAX(id)
FROM uzytkownicy_zdjecia
WHERE id_uzytkownika = u.id
AND prywatna =0) AS id_fotki,
(SELECT fotka
FROM uzytkownicy_zdjecia
WHERE id = id_fotki) AS fotka ,
(SELECT srednia_ocen
FROM uzytkownicy_zdjecia
WHERE id = id_fotki) AS srednia_ocen,
(SELECT ile_ocen
FROM uzytkownicy_zdjecia
WHERE id = id_fotki) AS ile_ocen
FROM uzytkownicy u
WHERE u.foto =1
AND u.plec = "mezczyzna"
ORDER BY srednia_ocen DESC,
ile_ocen DESC,
id_fotki DESC LIMIT 42
嘗試使用'Explain'來找出你需要索引的地方。 – Jens 2014-09-01 09:10:55
簡而言之,用['JOIN's](http://dev.mysql.com/doc/refman/5.0/en/join.html)替換子查詢。 – BlitZ 2014-09-01 09:11:51
我試過Join,它運行良好(0,6s),但我不得不添加GROUP BY或DISTINCT到這個,然後最好的時間是(2,5s),所以它仍然太長。 – user2828677 2014-09-01 09:14:27