mysql 选择速度慢
我有一个具有以下结构的表:
CREATE TABLE `game_entries` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`player_id` int(11) unsigned NOT NULL,
`game_id` int(11) unsigned NOT NULL,
`hero_id` int(11) unsigned NOT NULL,
`game_avg_pts` smallint(4) unsigned NOT NULL DEFAULT '0',
`game_season` tinyint(2) unsigned NOT NULL,
`game_length` smallint(11) unsigned NOT NULL,
`game_mode` char(10) NOT NULL,
`game_is_tb` tinyint(1) unsigned NOT NULL DEFAULT '0',
`game_map` char(10) NOT NULL,
`game_date` datetime NOT NULL,
`game_playersnum` tinyint(2) NOT NULL,
`side` tinyint(2) unsigned NOT NULL,
`won` tinyint(1) unsigned NOT NULL DEFAULT '0',
`pts` smallint(11) unsigned NOT NULL,
`pts_change` smallint(4) NOT NULL,
`kills` smallint(4) unsigned NOT NULL DEFAULT '0',
`deaths` smallint(4) unsigned NOT NULL DEFAULT '0',
`assists` smallint(4) unsigned NOT NULL DEFAULT '0',
`creeps` smallint(4) unsigned NOT NULL DEFAULT '0',
`towers` tinyint(2) unsigned NOT NULL DEFAULT '0',
`deleted_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `player_game` (`player_id`,`game_id`),
KEY `index_game` (`game_id`),
KEY `index_player` (`player_id`),
KEY `index_hero` (`hero_id`)
) ENGINE=InnoDB AUTO_INCREMENT=11706564 DEFAULT CHARSET=utf8;
该表有超过 11M 行。 当我尝试选择特定player_id的所有game_entries时,查询时间太长:
mysql> SELECT * FROM `game_entries` WHERE player_id = 4;
227 rows in set (1.68 sec)
mysql> SELECT * FROM `game_entries` WHERE player_id = 4 LIMIT 25;
25 rows in set (0.27 sec)
mysql> EXPLAIN SELECT * FROM `game_entries` WHERE player_id = 4;
+----+-------------+--------------+------+--------------------------+-------------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------+------+--------------------------+-------------+---------+-------+------+-------+
| 1 | SIMPLE | game_entries | ref | player_game,index_player | player_game | 4 | const | 226 | |
+----+-------------+--------------+------+--------------------------+-------------+---------+-------+------+-------+
1 row in set (0.00 sec)
有什么方法可以提高此查询的速度吗?
I have a table with the following structure:
CREATE TABLE `game_entries` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`player_id` int(11) unsigned NOT NULL,
`game_id` int(11) unsigned NOT NULL,
`hero_id` int(11) unsigned NOT NULL,
`game_avg_pts` smallint(4) unsigned NOT NULL DEFAULT '0',
`game_season` tinyint(2) unsigned NOT NULL,
`game_length` smallint(11) unsigned NOT NULL,
`game_mode` char(10) NOT NULL,
`game_is_tb` tinyint(1) unsigned NOT NULL DEFAULT '0',
`game_map` char(10) NOT NULL,
`game_date` datetime NOT NULL,
`game_playersnum` tinyint(2) NOT NULL,
`side` tinyint(2) unsigned NOT NULL,
`won` tinyint(1) unsigned NOT NULL DEFAULT '0',
`pts` smallint(11) unsigned NOT NULL,
`pts_change` smallint(4) NOT NULL,
`kills` smallint(4) unsigned NOT NULL DEFAULT '0',
`deaths` smallint(4) unsigned NOT NULL DEFAULT '0',
`assists` smallint(4) unsigned NOT NULL DEFAULT '0',
`creeps` smallint(4) unsigned NOT NULL DEFAULT '0',
`towers` tinyint(2) unsigned NOT NULL DEFAULT '0',
`deleted_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `player_game` (`player_id`,`game_id`),
KEY `index_game` (`game_id`),
KEY `index_player` (`player_id`),
KEY `index_hero` (`hero_id`)
) ENGINE=InnoDB AUTO_INCREMENT=11706564 DEFAULT CHARSET=utf8;
The table has more than 11M rows.
When I try to select all game_entries for a specific player_id, the query takes too long:
mysql> SELECT * FROM `game_entries` WHERE player_id = 4;
227 rows in set (1.68 sec)
mysql> SELECT * FROM `game_entries` WHERE player_id = 4 LIMIT 25;
25 rows in set (0.27 sec)
mysql> EXPLAIN SELECT * FROM `game_entries` WHERE player_id = 4;
+----+-------------+--------------+------+--------------------------+-------------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------+------+--------------------------+-------------+---------+-------+------+-------+
| 1 | SIMPLE | game_entries | ref | player_game,index_player | player_game | 4 | const | 226 | |
+----+-------------+--------------+------+--------------------------+-------------+---------+-------+------+-------+
1 row in set (0.00 sec)
Is there any way to improve the speed of this query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是在
player_id
列上添加索引。其次,如果可能的话,您可以在选择查询中提及有限字段而不是
*
One way is to add an index on
player_id
column.Second, if possible, you can mention the limited fields in select query instead of
*
考虑将
{player_id, game_id}
作为主键,而不是利用 innoDb 主键上的“集群”功能。这将使同一玩家的游戏记录彼此“靠近”,并使
player_id = ?
的搜索速度更快。Consider making
{player_id, game_id}
the primary key instead to take advantage of the "clustering" feature on innoDb primary key.That would keep game records for the same player "near" each other and make searches on
player_id = ?
faster.