我不是SQL专家。我需要优化以下查询:
SELECT trn.runner_id,
tr.race_id,
trr.position,
trr.program_number,
trn.horse_name,
trc.track_id,
tr.race_number,
trc.ticket_name,
trc.race_card_id,
trn.couple_type,
tt.country,
tr.post_time,
tr.sp_fav
FROM tb_race_results trr,
tb_races tr,
tb_race_runners trn,
tb_race_card trc,
tb_tracks tt
WHERE trr.race_id=514729
AND tr.race_id=trr.race_id
AND tr.race_card_id=trc.race_card_id
AND trr.program_number=trn.program_number
AND trn.race_id=trr.race_id
AND tt.track_id=trc.track_id
ORDER BY trr.position
我已经添加了必需的索引,但查询执行得很糟糕(约1秒)。
答案 0 :(得分:2)
SELECT trn.runner_id,
tr.race_id,
trr.position,
trr.program_number,
trn.horse_name,
trc.track_id,
tr.race_number,
trc.ticket_name,
trc.race_card_id,
trn.couple_type,
tt.country,
tr.post_time,
tr.sp_fav
FROM tb_race_results trr
INNER JOIN tb_races tr ON tr.race_id=trr.race_id
INNER JOIN tb_race_runners trn On trn.race_id=trr.race_id On trr.program_number=trn.program_number
INNER JOIN tb_race_card trc On tr.race_card_id=trc.race_card_id
INNER JOIN tb_tracks tt On tt.track_id=trc.track_id
WHERE trr.race_id=514729