我的查询加载时间有很大问题。在这种情况下,我需要 hg_ft_won (表格:值)列中的值 home_team_id 和 away_team_id (表格:匹配)。
它确实有效。加载只需要很长时间。有没有人有想法如何通过提供替代查询来改善它?
$sql = $conn->query("SELECT m.home_team_name,
m.away_team_name,
m.home_team_id,
m.away_team_id,
m.starting_time,
m.starting_date,
m.match_id,
m.season_id,
m.competition_id,
s.season_name,
s.country_name,
s.competition_name,
(SELECT hg_ft_won
FROM `values` v
WHERE m.home_team_id = v.team_id
AND m.season_id = v.season_id ) AS hg_ft_won1,
(SELECT hg_ft_won
FROM `values` v
WHERE m.away_team_id = v.team_id
AND m.season_id = v.season_id ) AS hg_ft_won2,
FROM matches m,
seasons s
WHERE m.season_id = s.id
AND m.starting_date = '2017-02-11'");
值表
匹配表
来自webpagetest.org的答案 0 :(得分:0)
从不在FROM
子句中使用逗号。 始终使用正确的,明确的JOIN
语法。这基本上是您的查询:
SELECT . . .
(SELECT hg_ft_won
FROM `values` v
WHERE m.home_team_id = v.team_id AND m.season_id = v.season_id
) AS hg_ft_won1,
(SELECT hg_ft_won
FROM `values` v
WHERE m.away_team_id = v.team_id AND
m.season_id = v.season_id
) AS hg_ft_won2,
FROM matches m JOIN
seasons s
ON m.season_id = s.id LEFT JOIN
competition
ON competition.id = ?.competition_id
WHERE m.starting_date = '2017-02-11'");
对于此查询,您需要索引:
matches(starting_date, season_id)
seasons(id)
(可能已经存在)competition(id)
(可能已经存在)values(team_id, season_id, hg_ft_won)
我不知道competition_id
的位置。它应该在最后一个键上添加到它所属的表中。
答案 1 :(得分:0)
尝试此查询。尽可能创建所需的索引。
SELECT m.home_team_name, m.away_team_name, m.home_team_id, m.away_team_id, m.starting_time, m.starting_date, m.match_id, m.season_id, m.competition_id, s.season_name, s.country_name, s.competition_name, v1.hg_ft_won AS hg_ft_won1, v2.hg_ft_won AS hg_ft_won2, FROM matches m INNER JOIN seasons s ON m.season_id = s.id LEFT JOIN `values` v1 ON m.home_team_id = v1.team_id AND m.season_id = v1.season_id LEFT JOIN `values` v2 ON m.home_team_id = v2.team_id AND m.season_id = v2.season_id AND m.starting_date = '2017-02-11'