MySQL Explain详解
2008-09-03 11:09:02 来源:WEB开发网4.type
这列很重要,显示了连接使用了哪种类别,有无使用索引.
从最好到最差的连接类型为const、eq_reg、ref、range、indexhe和ALL
(1).system
这是const联接类型的一个特例。表仅有一行满足条件.如下(t3表上的id是 primary key)
mysql>explainselect*from(select*fromt3whereid=3952602)a;
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
|id|select_type|table|type|possible_keys|key|key_len|ref|rows|Extra|
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
|1|PRIMARY||system|NULL|NULL|NULL|NULL|1||
|2|DERIVED|t3|const|PRIMARY,idx_t3_id|PRIMARY|4||1||
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
(2).const
表最多有一个匹配行,它将在查询开始时被读取。因为仅有一行,在这行的列值可被优化器剩余部分认为是常数。const表很快,因为它们只读取一次!
const用于用常数值比较PRIMARY KEY或UNIQUE索引的所有部分时。在下面的查询中,tbl_name可以用于const表:
select*fromtbl_namewhereprimary_key=1;
select*fromtbl_namewhereprimary_key_part1=1和primary_key_part2=2;
例如:
mysql>explainselect*fromt3whereid=3952602;
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
|id|select_type|table|type|possible_keys|key|key_len|ref|rows|Extra|
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
|1|SIMPLE|t3|const|PRIMARY,idx_t3_id|PRIMARY|4|const|1||
+----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
更多精彩
赞助商链接