mysql最佳化 mysql explain

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.