或运算符减慢MySQL查询速度

或运算符减慢MySQL查询速度,mysql,Mysql,我有四个表,每个表都通过一些字段与其他表关联:游戏、团队、替补名、国家 在ON子句中使用OR运算符编写此查询(请参见第6行),这大大降低了查询速度: select ROUND(AVG(attendance)), competition, team1, countries.* from games LEFT JOIN (teams INNER JOIN countries ON ( countries.iso3 = teams.country ) LEFT JOIN altn

我有四个表,每个表都通过一些字段与其他表关联:
游戏、团队、替补名、国家

在ON子句中使用OR运算符编写此查询(请参见第6行),这大大降低了查询速度:

select ROUND(AVG(attendance)), competition, team1, countries.* from games 
LEFT JOIN (teams 
    INNER JOIN countries ON ( countries.iso3 = teams.country ) 
    LEFT JOIN altnames ON ( altnames.entityType = "team" AND altnames.season = "1011" AND altnames.entityId = teams.longName )  
) 
ON ( altnames.altValue = games.team1 OR teams.longName = games.team1 )
where games.season="1011" group by games.competition, games.team1 having AVG(attendance)>= 500 order by AVG(attendance) desc
当不使用OR时,查询速度足够快,一次只使用两个条件中的一个:

一,

二,

知道为什么会发生这种情况,以及如何加快第一个查询的速度吗


编辑:

以下是表格:

其他名称:

CREATE TABLE IF NOT EXISTS `altnames` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `entityType` enum('team','comp','logo') NOT NULL,
  `entityId` varchar(50) NOT NULL DEFAULT '0',
  `season` varchar(4) NOT NULL DEFAULT '0',
  `altValue` varchar(50) NOT NULL DEFAULT '0',
  KEY `id` (`id`),
  KEY `entityType_season_altValue` (`entityType`,`season`,`altValue`)
)
国家:

CREATE TABLE IF NOT EXISTS `countries` (
  `name` varchar(50) NOT NULL,
  `iso2` varchar(2) NOT NULL,
  `iso3` varchar(3) NOT NULL,
  `color` varchar(7) NOT NULL,
  KEY `iso3` (`iso3`)
)
游戏:

CREATE TABLE IF NOT EXISTS `games` (
  `id` int(7) NOT NULL AUTO_INCREMENT,
  `competition` varchar(10) NOT NULL,
  `season` varchar(4) NOT NULL,
  `stage` varchar(10) DEFAULT NULL,
  `gamedate` date DEFAULT NULL,
  `team1` varchar(50) NOT NULL,
  `team2` varchar(50) NOT NULL,
  `score1` tinyint(2) DEFAULT NULL,
  `score2` tinyint(2) DEFAULT NULL,
  `attendance` int(11) DEFAULT NULL,
  `location` varchar(100) DEFAULT NULL,
  `source` varchar(400) DEFAULT NULL,
  `altsource` varchar(400) DEFAULT NULL,
  `note` varchar(400) DEFAULT NULL,
  KEY `id` (`id`),
  KEY `competition_season` (`competition`,`season`),
  KEY `team_comp_season` (`team1`,`competition`,`season`)
)
小组:

CREATE TABLE IF NOT EXISTS `teams` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `shortName` varchar(20) DEFAULT NULL,
  `longName` varchar(50) NOT NULL,
  `urlSlug` varchar(50) DEFAULT NULL,
  `country` varchar(3) NOT NULL DEFAULT 'NSP',
  `competitions` varchar(50) DEFAULT NULL,
  `latitude` float(10,6) DEFAULT '0.000000',
  `longitude` float(10,6) DEFAULT '0.000000',
  `inactive` tinyint(1) DEFAULT '0',
  KEY `id` (`id`),
  KEY `long` (`longName`),
  KEY `FK_teams_countries` (`country`),
  CONSTRAINT `teams_ibfk_1` FOREIGN KEY (`country`) REFERENCES `countries` (`iso3`)
)

我只是在等待专家意见时想到了一个有趣的主意, 尝试使用和切换或操作员

条件A或条件B

相当于

非(非(条件A)和非(条件B))


祝你好运,

使用
解释
查看执行计划。最有可能的情况是,
在两个不同的列上处于on状态,这意味着MySQL没有有效地利用索引。有时,我们可以通过将两个有效查询的结果与
UNION ALL
集合运算符相结合来获得更好的性能。哪个表是
考勤
?您应该为每一列使用表前缀,所以我们不需要猜测。当表格不明显且无法从列名派生时,还应发布模式并描述表格之间的关系。@Spiegel Attention是游戏中的一个字段,而不是表格。同样,竞争和团队1也是如此。@spencer7593不幸的是,我不知道该如何解释解释解释:你看过相关的MySQL文档了吗?或者,我会尝试使用联合(使用适当的重复数据消除)绕过问题。您好,欢迎使用堆栈溢出。不清楚为什么这会有帮助,请编辑答案以提供更多信息。请尝试。还是很慢。
CREATE TABLE IF NOT EXISTS `games` (
  `id` int(7) NOT NULL AUTO_INCREMENT,
  `competition` varchar(10) NOT NULL,
  `season` varchar(4) NOT NULL,
  `stage` varchar(10) DEFAULT NULL,
  `gamedate` date DEFAULT NULL,
  `team1` varchar(50) NOT NULL,
  `team2` varchar(50) NOT NULL,
  `score1` tinyint(2) DEFAULT NULL,
  `score2` tinyint(2) DEFAULT NULL,
  `attendance` int(11) DEFAULT NULL,
  `location` varchar(100) DEFAULT NULL,
  `source` varchar(400) DEFAULT NULL,
  `altsource` varchar(400) DEFAULT NULL,
  `note` varchar(400) DEFAULT NULL,
  KEY `id` (`id`),
  KEY `competition_season` (`competition`,`season`),
  KEY `team_comp_season` (`team1`,`competition`,`season`)
)
CREATE TABLE IF NOT EXISTS `teams` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `shortName` varchar(20) DEFAULT NULL,
  `longName` varchar(50) NOT NULL,
  `urlSlug` varchar(50) DEFAULT NULL,
  `country` varchar(3) NOT NULL DEFAULT 'NSP',
  `competitions` varchar(50) DEFAULT NULL,
  `latitude` float(10,6) DEFAULT '0.000000',
  `longitude` float(10,6) DEFAULT '0.000000',
  `inactive` tinyint(1) DEFAULT '0',
  KEY `id` (`id`),
  KEY `long` (`longName`),
  KEY `FK_teams_countries` (`country`),
  CONSTRAINT `teams_ibfk_1` FOREIGN KEY (`country`) REFERENCES `countries` (`iso3`)
)