[英]Select values from another table - too slow
I have a huge problem with the loading time for my query. 我的查询加载时间有很大问题。 In this case I need values from the column hg_ft_won (table: values) for the home_team_id and away_team_id (table: matches).
在这种情况下,我需要来自home_team_id和away_team_id (table:matches)的列hg_ft_won (table:values)的值。
It does work as it should. 它确实有效。 It only takes really long to load.
加载只需要很长时间。 Does anyone have ideas how to improve that by having an alternative query?
有没有人有想法如何通过提供替代查询来改善它?
$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'");
values table 价值表
matches table 匹配表
Never use commas in the FROM
clause. 切勿在
FROM
子句中使用逗号。 Always use proper, explicit JOIN
syntax. 始终使用正确,明确的
JOIN
语法。 This is essentially your query: 这基本上是您的查询:
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'");
For this query, you want indexes on: 对于此查询,您需要索引:
matches(starting_date, season_id)
seasons(id)
(probably already there) seasons(id)
(可能已经存在) competition(id)
(probably already there) competition(id)
(可能已经存在) values(team_id, season_id, hg_ft_won)
I don't know where competition_id
is. 我不知道
competition_id
在哪里。 It should be added at the last key to whichever table it belongs to. 它应该在最后一个键中添加到它所属的表中。
Have a try on this query. 试试这个查询。 Create the required index wherever possible.
尽可能创建所需的索引。
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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.