標籤:style blog http color io os 使用 ar strong
先看一下arena_match_index的表結構,大家注意表的索引結構
CREATE TABLE `arena_match_index` (
`tid` int(10) unsigned NOT NULL DEFAULT ‘0‘,
`mid` int(10) unsigned NOT NULL DEFAULT ‘0‘,
`group` int(10) unsigned NOT NULL DEFAULT ‘0‘,
`round` tinyint(3) unsigned NOT NULL DEFAULT ‘0‘,
`day` date NOT NULL DEFAULT ‘0000-00-00‘,
`begintime` datetime NOT NULL DEFAULT ‘0000-00-00 00:00:00‘,
UNIQUE KEY `tm` (`tid`,`mid`),
KEY `mid` (`mid`),
KEY `begintime` (`begintime`),
KEY `dg` (`day`,`group`),
KEY `td` (`tid`,`day`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
接著看下面的sql:
SELECT round FROM arena_match_index WHERE `day` = ‘2010-12-31‘ AND `group` = 18 AND`begintime` < ‘2010-12-31 12:14:28‘ order by begintime LIMIT 1;
這條sql的查詢條件顯示可能使用的索引有`begintime`和`dg`,但是由於使用了order by begintime排序mysql最後選擇使用`begintime`索引,explain的結果為:
mysql> explain SELECT round FROM arena_match_index WHERE `day` = ‘2010-12-31‘ AND `group` = 18 AND `begintime` < ‘2010-12-31 12:14:28‘ order by begintime LIMIT 1;
+----+-------------+-------------------+-------+---------------+-----------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+-------+---------------+-----------+---------+------+--------+-------------+
| 1 | SIMPLE | arena_match_index | range | begintime,dg | begintime | 8 | NULL |226480 | Using where |
+----+-------------+-------------------+-------+---------------+-----------+---------+------+--------+-------------+
explain的結果顯示使用`begintime`索引要掃描22w條記錄,這樣的查詢效能是非常糟糕的,實際的執行情況也是初次執行(還未有快取資料時)時需要30秒以上的時間。
實際上這個查詢使用`dg`聯合索引的效能更好,因為同一天同一個小組內也就幾十場比賽,因此應該優先使用`dg`索引定位到匹配的資料集合再進行排序,那麼如何告訴mysql使用指定索引呢?使用use index語句:
mysql> explain SELECT round FROM arena_match_index use index (dg) WHERE `day` = ‘2010-12-31‘ AND `group` = 18 AND `begintime` < ‘2010-12-31 12:14:28‘ order by begintime LIMIT 1;
+----+-------------+-------------------+------+---------------+------+---------+-------------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+------+---------------+------+---------+-------------+------+-----------------------------+
| 1 | SIMPLE | arena_match_index | ref | dg | dg | 7 | const,const | 757 | Using where; Using filesort |
+----+-------------+-------------------+------+---------------+------+---------+-------------+------+-----------------------------+
explain結果顯示使用`dg`聯合索引只需要掃描757條資料,效能直接提升了上百倍,實際的執行情況也是幾乎立即就返回了查詢結果。
在最初的查詢語句中只要把order by begintime去掉,mysql就會使用`dg`索引了,再次印證了order by會影響mysql的索引選擇策略!
mysql> explain SELECT round FROM arena_match_index WHERE `day` = ‘2010-12-31‘ AND `group` = 18 AND `begintime` < ‘2010-12-31 12:14:28‘ LIMIT 1;
+----+-------------+-------------------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+------+---------------+------+---------+-------------+------+-------------+
| 1 | SIMPLE | arena_match_index | ref | begintime,dg | dg | 7 | const,const | 717 | Using where |
+----+-------------+-------------------+------+---------------+------+---------+-------------+------+-------------+
通過上面的例子說mysql有時候也並不聰明,並非總能做出最優選擇,還是需要我們開發人員對它進行“調教”!
mysql explainhttp://www.douban.com/note/312979020/http://wenku.baidu.com/link?url=RU4DHmGILETevxWw_F_dQKvxKvbcU2HOZI1Lohitf5h8IOaIIUfkseAdjs8dp0efldMqymURbGyRxbos6MNIPCR5ziry5lI5ZFOvVOv7kVK http://www.jb51.net/article/39221.htm
mysql最佳化 mysql explain