2011-02-16 112 views
0

我有以下結構的表:慢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; 

表已超過1100萬行。 當我嘗試選擇所有game_entries特定player_id,查詢時間過長:

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) 

有什麼辦法改善查詢的速度?

+1

第一件事選擇所需的字段不要添加`*` – 2011-02-16 09:46:58

回答

0

考慮將{player_id, game_id}的主鍵,而不是採取InnoDB的主鍵優勢"clustering" feature的。

這樣可以保持同一玩家的遊戲記錄彼此「接近」,並更快地搜索player_id = ?

2

一種方法是在player_id列中添加索引。

其次,如果可能的話,你可以選擇查詢何況有限的區域,而不是*

+0

但是`KEY index_player(player_id)`,它不是`player_id`列的索引嗎? – tycooon 2011-02-16 09:55:05