Mysql
 sql >> Base de données >  >> RDS >> Mysql

Optimiser la requête de classement lente

Essayez ceci :

SELECT  t.*, cnt
FROM    (
        SELECT  team_id, COUNT(*) AS cnt
        FROM    (
                SELECT  player_id, MAX(id) AS mid
                FROM    player_team
                GROUP BY
                        player_id
                ) q
        JOIN    player_team pt
        ON      pt.id = q.mid
        GROUP BY
                team_id
        ) q2
JOIN    team t
ON      t.id = q2.team_id
ORDER BY
        cnt DESC
LIMIT 50

Créer un index sur player_team (player_id, id) (dans cet ordre) pour que cela fonctionne rapidement.