msyql中Explain的用法详解
发布:smiling 来源: PHP粉丝网 添加日期:2014-10-10 10:41:33 浏览: 评论:0
- mysql> explain select * from t3 where id = (select id from t3 where id=3952602 ) ;
- +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+
- | 1 | PRIMARY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | |
- | 2 | SUBQUERY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | Using index |
- +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------------+
(6).DEPENDENT SUBQUERY
子查询中的第一个SELECT,取决于外面的查询,代码如下:
- mysql> explain select id from t3 where id in (select id from t3 where id=3952602 ) ;
- +----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+ --phpfensi.com
- | 1 | PRIMARY | t3 | index | NULL | PRIMARY | 4 | NULL | 1000 | Using where; Using index |
- | 2 | DEPENDENT SUBQUERY | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | const | 1 | Using index |
- +----+--------------------+-------+-------+-------------------+---------+---------+-------+------+--------------------------+
(7).DERIVED
派生表的SELECT(FROM子句的子查询),代码如下:
- mysql> explain select * from (select * from t3 where id=3952602) a ;
- +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
- | 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | |
- | 2 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |
- +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
3.table
显示这一行的数据是关于哪张表的,有时不是真实的表名字,看到的是derivedx(x是个数字,我的理解是第几步执行的结果),代码如下:
- mysql> explain select * from (select * from ( select * from t3 where id=3952602) a) b;
- +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
- | 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | |
- | 2 | DERIVED | <derived3> | system | NULL | NULL | NULL | NULL | 1 | |
- | 3 | DERIVED | t3 | const | PRIMARY,idx_t3_id | PRIMARY | 4 | | 1 | |
- +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
4.type
这列很重要,显示了连接使用了哪种类别,有无使用索引,从最好到最差的连接类型为const、eq_reg、ref、range、indexhe和ALL
(1).system
这是const联接类型的一个特例,表仅有一行满足条件,如下(t3表上的id是 primary key),代码如下:
- mysql> explain select * from (select * from t3 where id=3952602) a ;
- +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
- | 1 | PRIMARY | <derived2> | 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 * from tbl_name WHERE primary_key=1;
SELECT * from tbl_name WHERE primary_key_part1=1和 primary_key_part2=2;
例如:
- mysql> explain select * from t3 where id=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 | |
- +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+
(3).eq_ref
对于每个来自于前面的表的行组合,从该表中读取一行,这可能是最好的联接类型,除了const类型,它用在一个索引的所有部分被联接使用并且索引是UNIQUE或PRIMARY KEY.
eq_ref可以用于使用= 操作符比较的带索引的列,比较值可以为常量或一个使用在该表前面所读取的表的列的表达式。
在下面的例子中,MySQL可以使用eq_ref联接来处理ref_tables,代码如下:
- SELECT * FROM ref_table,other_table
- WHERE ref_table.key_column=other_table.column;
- SELECT * FROM ref_table,other_table
- WHERE ref_table.key_column_part1=other_table.column
- AND ref_table.key_column_part2=1;
- 例如
- mysql> create unique index idx_t3_id on t3(id) ;
- Query OK, 1000 rows affected (0.03 sec)
- Records: 1000 Duplicates: 0 Warnings: 0
- mysql> explain select * from t3,t4 where t3.id=t4.accountid;
- +----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+
- | 1 | SIMPLE | t4 | ALL | NULL | NULL | NULL | NULL | 1000 | |
- | 1 | SIMPLE | t3 | eq_ref | PRIMARY,idx_t3_id | idx_t3_id | 4 | dbatest.t4.accountid | 1 | |
- +----+-------------+-------+--------+-------------------+-----------+---------+----------------------+------+-------+
(4).ref
对于每个来自于前面的表的行组合,所有有匹配索引值的行将从这张表中读取,如果联接只使用键的最左边的前缀,或如果键不是UNIQUE或PRIMARY KEY(换句话说,如果联接不能基于关键字选择单个行的话),则使用ref。如果使用的键仅仅匹配少量行,该联接类型是不错的.
Tags: msyql Explain用法
相关文章
- ·windows命令行msyqldump导出数据库备份(2014-10-08)
- ·mysql explain 用法详解(2014-10-14)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)