我有這個查詢,它增加了一個'stats'表的值的負載。當查詢運行時,它會選擇這些值來填充子查詢中的表。我想知道是否可以更有效地做出這種做法,或者如果我正在做一些非常錯誤的事情。我不是那麼熟悉MySQL的所以任何幫助將是巨大的:)MySQL子查詢效率
下面是該查詢:
UPDATE mediastats SET
mediastats_members = (SELECT count(*) FROM status WHERE status_media_id = :id),
mediastats_avscore = (SELECT AVG(status_rating) FROM status WHERE status_media_id = :id),
mediastats_done = (SELECT count(*) FROM status WHERE status_status = 'done' AND status_media_id = :id),
mediastats_doing = (SELECT count(*) FROM status WHERE status_status = 'doing' AND status_media_id = :id),
mediastats_redoing = (SELECT count(*) FROM status WHERE status_status = 'redoing' AND status_media_id = :id),
mediastats_dropped = (SELECT count(*) FROM status WHERE status_status = 'dropped' AND status_media_id = :id),
mediastats_wantto = (SELECT count(*) FROM status WHERE status_status = 'wantto' AND status_media_id = :id),
mediastats_wont = (SELECT count(*) FROM status WHERE status_status = 'wont' AND status_media_id = :id),
mediastats_stalled = (SELECT count(*) FROM status WHERE status_status = 'stalled' AND status_media_id = :id),
mediastats_rating_1 = (SELECT count(*) FROM status WHERE status_rating = 1 AND status_media_id = :id),
mediastats_rating_2 = (SELECT count(*) FROM status WHERE status_rating = 2 AND status_media_id = :id),
mediastats_rating_3 = (SELECT count(*) FROM status WHERE status_rating = 3 AND status_media_id = :id),
mediastats_rating_4 = (SELECT count(*) FROM status WHERE status_rating = 4 AND status_media_id = :id),
mediastats_rating_5 = (SELECT count(*) FROM status WHERE status_rating = 5 AND status_media_id = :id),
mediastats_rating_6 = (SELECT count(*) FROM status WHERE status_rating = 6 AND status_media_id = :id),
mediastats_rating_7 = (SELECT count(*) FROM status WHERE status_rating = 7 AND status_media_id = :id),
mediastats_rating_8 = (SELECT count(*) FROM status WHERE status_rating = 8 AND status_media_id = :id),
mediastats_rating_9 = (SELECT count(*) FROM status WHERE status_rating = 9 AND status_media_id = :id),
mediastats_rating_10 = (SELECT count(*) FROM status WHERE status_rating = 10 AND status_media_id = :id)
WHERE mediastats_media_id = :id
的:ID是從PHP加入。
謝謝你真棒:)這種方法比原來的優點是什麼? – Humphrey
其優點是它可以計算表中一次通過的所有計數。 –