我有一个查询
SELECT
"athlete"."id" AS "athlete_id"
FROM "athlete"
LEFT JOIN "athlete_in_game" ON athlete.id = athlete_in_game.id_athlete
LEFT JOIN "athlete_in_team" ON athlete.id = athlete_in_team.id_athlete
LEFT JOIN "game" ON
athlete_in_team.id_team = game.id_team_home OR
athlete_in_team.id_team = game.id_team_away OR
athlete_in_game.id_game = game.id
LEFT JOIN "sport_competition" ON sport_competition.id_game = game.id
GROUP BY "athlete"."id"
我需要选择参加比赛的所有运动员。但可能是表格中没有数据" athlete_in_game"对于几场比赛而言,在这种情况下,我从表格中选出所有运动员" athlete_in_team"对于这些游戏。因此,我使用双" OR"在第三个左边加入。我需要所有书面条件,但因为双重" OR"它可能工作太久了。是否有机会优化它?
答案 0 :(得分:0)
我认为首先你必须首先改进模式。团队相关信息应该是单独的表。和游戏应该是分开的。我认为你不必左右加入game
表。只允许在athlete_in_game
和athlete_in_team
上加入联盟就足够了。喜欢所以
SELECT
"athlete"."id" AS "athlete_id"
FROM "athlete"
LEFT JOIN "athlete_in_game" ON athlete.id = athlete_in_game.id_athlete
LEFT JOIN "athlete_in_team" ON athlete.id = athlete_in_team.id_athlete
JOIN "game" ON
athlete_in_team.id_team = game.id_team_home OR
athlete_in_team.id_team = game.id_team_away OR
athlete_in_game.id_game = game.id
LEFT JOIN "sport_competition" ON sport_competition.id_game = game.id
GROUP BY "athlete"."id"