MySql學習筆記(六):掃描範圍

explain的type列表示該條查詢的掃描範圍,一共有七種,效果由上到下排列:mysql

system>const>eq_ref>ref>range>index>all。sql

數據準備:ide

CREATE TABLE `t_blog` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(50) default NULL,
  `typeId` int(11) default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `title_index` (`title`),
  KEY `type_index` (`typeId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `t_type` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(20) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

一、systemblog

該表中只有一行記錄,這種狀況在平常開發中不多見,很少贅述;索引

二、const開發

表示經過索引一次就找到告終果,用於掃描主鍵和惟一索引,例如:rem

mysql> explain select * from t_blog where id = 1;
+----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table  | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t_blog | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+
1 row in set

在where子句中,id爲主鍵且值爲一個常數,在id索引中只有一條數據與之對應。it

三、eq_reftable

經過主鍵和惟一索引,只有一條數據與之匹配,例如:class

mysql> explain select b.* from t_blog b left join t_type t on b.typeId = t.id;
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+
| id | select_type | table | type   | possible_keys | key     | key_len | ref           | rows | Extra       |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+
|  1 | SIMPLE      | b     | ALL    | NULL          | NULL    | NULL    | NULL          |    7 |             |
|  1 | SIMPLE      | t     | eq_ref | PRIMARY       | PRIMARY | 4       | blog.b.typeId |    1 | Using index |
+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+
2 rows in set

eq_ref和const都表示在惟一索引或主鍵的做用下,只找到一行與之匹配的數據。const表示按主鍵和惟一索引讀取,eq_ref一般體如今連表上,按連表的主鍵和惟一索引讀取。

四、ref

非惟一索引掃描,有多個行與之匹配

mysql> explain select * from t_blog where typeId = 4;
+----+-------------+--------+------+---------------+------------+---------+-------+------+-------------+
| id | select_type | table  | type | possible_keys | key        | key_len | ref   | rows | Extra       |
+----+-------------+--------+------+---------------+------------+---------+-------+------+-------------+
|  1 | SIMPLE      | t_blog | ref  | type_index    | type_index | 5       | const |    1 | Using where |
+----+-------------+--------+------+---------------+------------+---------+-------+------+-------------+
1 row in set

typeId是表的普通索引,即非惟一索引,與eq_ref最大的區別在於ref表示非惟一索引掃描。

五、range

表示範圍,使用索引選擇行,使用了  > < in beteen等

mysql> EXPLAIN select * from t_blog where id>2;
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table  | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | t_blog | range | PRIMARY       | PRIMARY | 4       | NULL |    3 | Using where |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
1 row in set

六、index

遍歷索引樹,讀全表

mysql> EXPLAIN select id from t_blog;
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table  | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | t_blog | index | NULL          | PRIMARY | 4       | NULL |    7 | Using index |
+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
1 row in set

只查詢id,因此只遍歷索引文件便可,不須要從硬盤中讀取,比all快。

七、all

讀全表,不使用任何索引,從硬盤中讀數據,最慢

mysql> explain 
select * from t_blog;
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | t_blog | ALL  | NULL          | NULL | NULL    | NULL |    7 |       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------+
1 row in set


*在通常的開發過程當中,達到ref便可

相關文章
相關標籤/搜索