我的 Nexuiz Statistics Web 项目中的 MySQL 请求有问题。我不想实时生成统计表,请求会浪费时间(有 310000 个条目,请求需要 3 到 7 秒)这是我要优化的 SQL 请求等。
SELECT n.name, d.times_kill, d.times_dead, d.kd, d.times_cap, d.points
FROM
( SELECT f.player player, f.times_kill, f.times_dead, f.kd, g.times_cap, ((f.points * (7 / 3)) + POW(g.times_cap, 1.5)) points
FROM
( SELECT k.player player, k.times_kill, d.times_dead, (k.times_kill / d.times_dead) kd, ((k.times_kill / d.times_dead) * k.times_kill) points
FROM
( SELECT player, count( * ) times_kill
FROM `nexstat`.`dc_events`
WHERE event = 'KILL' AND server = '2'
GROUP BY player
ORDER BY times_kill DESC
) k
JOIN
( SELECT player, count( * ) times_dead
FROM `nexstat`.`dc_events`
WHERE event = 'DEAD' AND server = '2'
GROUP BY player
ORDER BY times_dead DESC
) d
ON d.player = k.player
ORDER BY points DESC
) f
JOIN
( SELECT player, count( * ) times_cap
FROM `nexstat`.`dc_events`
WHERE event = 'CAP' AND server = '2'
GROUP BY player ORDER BY times_cap DESC
) g
ON f.player = g.player
) d
JOIN
( SELECT * FROM `nexstat`.`dc_players` WHERE main = 1
) n
ON d.player = n.id
GROUP BY id
ORDER BY points DESC
数据库模型在这里:
CREATE TABLE IF NOT EXISTS `dc_events` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`player` int(11) NOT NULL,
`event` enum('CAP','KILL','DEAD','DROP','PICKUP','CHANGE','JOIN','LEAVE') NOT NULL,
`param0` int(11) DEFAULT NULL,
`server` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `event` (`event`),
KEY `server` (`server`),
KEY `player` (`player`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ROW_FORMAT=FIXED AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `dc_players` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`main` tinyint(1) NOT NULL DEFAULT '1',
`name` varchar(512) NOT NULL,
`website` varchar(512) NOT NULL,
`email` varchar(512) NOT NULL,
`clan` varchar(512) NOT NULL,
`country` varchar(2) NOT NULL,
UNIQUE KEY `name` (`name`),
KEY `website` (`website`),
KEY `email` (`email`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
如果有人帮助我会很酷;)