我有一个定期触发的事件来“中止”一些废弃的游戏(一个简单的匹配服务器)。
事实证明,此更新非常慢(非常),我正在寻求将其做得更好的建议。
问题更新:
UPDATE user SET skill=skill+
(SELECT count(participant_1) * 25 FROM matches
WHERE score_2 IS NULL
AND score_2_time IS NOT NULL
AND participant_1=user.id
AND score_2_time < (NOW() - INTERVAL 1 HOUR)
AND status=0);
匹配表:
matches CREATE TABLE `matches` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`match_hash` varchar(64) DEFAULT NULL,
`skill` int(10) unsigned DEFAULT NULL,
`status` int(10) unsigned DEFAULT NULL,
`participant_1` int(10) unsigned DEFAULT NULL,
`score_1` int(10) unsigned DEFAULT NULL,
`score_1_time` timestamp NULL DEFAULT NULL,
`participant_1_rematched` tinyint(4) DEFAULT NULL,
`participant_2` int(10) unsigned DEFAULT NULL,
`score_2` int(10) unsigned DEFAULT NULL,
`score_2_time` timestamp NULL DEFAULT NULL,
`participant_2_rematched` tinyint(4) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
`finished_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=40667 DEFAULT CHARSET=latin1
用户表:
user CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`skill` int(10) unsigned DEFAULT NULL,
`created` timestamp NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=1876 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
非常感谢任何指导。
答案 0 :(得分:0)
您需要在matches
表上有更多的索引,当然至少需要participant_1
,并且在WHERE子句中提及的其他需要帮助的地方。出于完整性考虑,participant_1
可能是user.id
的外键。
答案 1 :(得分:-1)
尝试此查询:
update user a join (SELECT participant_1,count(participant_1) * 25 as count FROM matches
WHERE score_2 IS NULL
AND score_2_time IS NOT NULL
AND score_2_time < (NOW() - INTERVAL 1 HOUR)
AND status=0 group by participant_1) b on a.id=b.participant_1 SET a.skill=a.skill+b.count