mysql explain的使用说明

来源:互联网 发布:网络打印机smb扫描 编辑:程序博客网 时间:2024/06/06 11:36
-- Table "class" DDL

CREATE TABLE `class` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) default NULL,
PRIMARY KEY (`id`),
KEY `i_id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


-- Table "student" DDL

CREATE TABLE `student` (
`studentId` varchar(11) NOT NULL,
`name` varchar(50) default NULL,
PRIMARY KEY (`studentId`),
KEY `i_studentId` (`studentId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


-- Table "student_class" DDL

CREATE TABLE `student_class` (
`id` int(11) NOT NULL auto_increment,
`classId` int(11) default NULL,
`studentId` varchar(11) default NULL,
`description` varchar(255) default NULL,
PRIMARY KEY (`id`),
KEY `fk_studentId` (`studentId`),
KEY `fk_student_classId` (`classId`),
CONSTRAINT `fk_student_classId` FOREIGN KEY (`classId`) REFERENCES `class` (`id`),
CONSTRAINT `fk_studentId` FOREIGN KEY (`studentId`) REFERENCES `student` (`studentId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


-- Table "teacher" DDL

CREATE TABLE `teacher` (
`teacherId` varchar(11) NOT NULL default '',
`name` varchar(50) default NULL,
PRIMARY KEY (`teacherId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


-- Table "teacher_class" DDL

CREATE TABLE `teacher_class` (
`id` int(11) NOT NULL auto_increment,
`teacherId` varchar(11) default NULL,
`classId` int(11) default NULL,
`description` varchar(255) default NULL,
PRIMARY KEY (`id`),
KEY `fk_teacherId` (`teacherId`),
KEY `fk_teacher_classId` (`classId`),
CONSTRAINT `fk_teacher_classId` FOREIGN KEY (`classId`) REFERENCES `class` (`id`),
CONSTRAINT `fk_teacherId` FOREIGN KEY (`teacherId`) REFERENCES `teacher` (`teacherId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO class(name) values("04");
INSERT INTO class(name) values("05");

INSERT INTO student(studentId, name) values("100001", "张三");
INSERT INTO student(studentId, name) values("100002", "李四");
INSERT INTO student(studentId, name) values("100003", "wangwu");

INSERT INTO student_class(studentId, classId) values("100001", 1);
INSERT INTO student_class(studentId, classId) values("100002", 2);
INSERT INTO student_class(studentId, classId) values("100003", 2);

INSERT INTO teacher(teacherId, name) values("900001", "李老师");
INSERT INTO teacher(teacherId, name) values("900002", "王老师");

INSERT INTO teacher_class(teacherId, classId) values("900001", 1);
INSERT INTO teacher_class(teacherId, classId) values("900002", 2);



mysql explain的使用说明
explain显示了mysql如何使用索引来处理select语句以及连接表。可以帮助选择更好的索引和写出更优化的查询语句
使用方法,在select语句前加上explain就可以了:
如:SELECT A.name, B.name FROM student A, class B, student_class C WHERE B.id = 2 AND B.Id = C.classId AND A.studentId = C.studentId;
分析结果形式如下:

引用

mysql> EXPLAIN SELECT A.name, B.name FROM student A, class B, student_class C WHERE B.id = 2 AND B.Id = C.classId AND A.studentId = C.studentId;
+----+-------------+-------+-------+---------------------------------+--------------------+---------+------------------+------+-------------+
| id | select_type | table | type | possible_keys                   | key                | key_len | ref              | rows | Extra       |
+----+-------------+-------+-------+---------------------------------+--------------------+---------+------------------+------+-------------+
| 1 | SIMPLE      | B     | const | PRIMARY,i_id                    | PRIMARY            | 4       | const            |    1 |             |
| 1 | SIMPLE      | C     | ref   | fk_studentId,fk_student_classId | fk_student_classId | 5       | const            |    1 | Using where |
| 1 | SIMPLE      | A     | ref   | PRIMARY,i_studentId             | PRIMARY            | 35      | user.C.studentId |    1 |             |
+----+-------------+-------+-------+---------------------------------+--------------------+---------+------------------+------+-------------+
3 rows in set (0.00 sec)


EXPLAIN列的解释:
MYSQL 5.1 用户手册 7.2.1. EXPLAIN语法(获取SELECT相关信息)
有详细的诠释

id: SELECT识别符。这是SELECT的查询序列号。
select_type: 可以为以下任何一种
    SIMPLE: 简单SELECT(不使用UNION或子查询)
    PRIMARY: 最外面的SELECT
    UNION: UNION中的第二个或后面的SELECT语句
    DEPENDENT UNION: 子查询中的第一个SELECT,取决于外面的查询
    DERIVED: 导出表的SELECT(FROM子句的子查询)


table 显示这一行的数据是关于哪张表的
type 这是重要的列,显示连接使用了何种联接类型。从最好到最差的连接类型为system, cons,eq_ref, ref, ref_or_null, index_merge, unique_subquery, index_subquery, range、index和ALL
possible_keys 显示可能应用在这张表中的索引。如果为NULL,没有可能的索引。可以为相关的域从WHERE语句中选择一个合适的语句
key 实际使用的索引。如果为NULL,则没有使用索引。很少的情况下,MYSQL会选择优化不足的索引。这种情况下,可以在SELECT语句中使用USE INDEX(indexname)来强制使用一个索引或者用IGNORE INDEX(indexname)来强制MYSQL忽略索引
key_len 使用的索引的长度。在不损失精确性的情况下,长度越短越好
ref 显示索引的哪一列被使用了,如果可能的话,是一个常数
rows MYSQL认为必须检查的用来返回请求数据的行数
Extra 关于MYSQL如何解析查询的额外信息。将在表4.3中讨论,但这里可以看到的坏的例子是Using temporary和Using filesort,意思MYSQL根本不能使用索引,结果是检索会很慢

extra 该列包含MySQL解决查询的详细信息。
    Distinct: 一旦MYSQL找到了与行相联合匹配的行,就不再搜索了
    Not exists: MYSQL优化了LEFT JOIN,一旦它找到了匹配LEFT JOIN标准的行,就不再搜索了
    Range checked for each:    Record(index map:#)没有找到理想的索引,因此对于从前面表中来的每一 个行组合,MYSQL检查使用哪个索引,并用它来从表中返回行。这是使用索引的最慢的连接之一
    Using filesort: 看到这个的时候,查询就需要优化了。MYSQL需要进行额外的步骤来发现如何对返回的行 排序。它根据连接类型以及存储排序键值和匹配条件的全部行的行指针来排序全部行
    Using index: 列数据是从仅仅使用了索引中的信息而没有读取实际的行动的表返回的,这发生在对表的全 部的请求列都是同一个索引的部分的时候
    Using temporary: 看到这个的时候,查询需要优化了。这里,MYSQL需要创建一个临时表来存储结果,这通常发生在对不同的列集进行ORDER BY上,而不是GROUP BY上
   Using where: WHERE子句用于限制哪一个行匹配下一个表或发送到客户。除非你专门从表中索取或检查 所有行,如果Extra值不为Using where并且表联接类型为ALLindex,查询可能会有一些错误

如果想要使查询尽可能快,应找出Using filesort 和Using temporary的Extra值
Using sort_union(...), Using union(...), Using intersect(...): 这些函数说明如何为index_merge联接类型合并索引扫描。
Using index for group-by: 类似于访问表的Using index方式,Using index for group-by表示MySQL发现了一个索引,可以用来查询GROUP BYDISTINCT查询的所有列,而不要额外搜索硬盘访问实际的表。并且,按最有效的方式使用索引,以便对于每个组,只读取少量索引条目

不同连接类型的诠释(按照效率高低的顺序排序)
system: 表只有一行:system表。这是const连接类型的特殊情况
const: 表中的一个记录的最大值能够匹配这个查询(索引可以是主键或惟一索引)。因为只有一行,这个值实际就是常数,因为MYSQL先读这个值然后把它当做常数来对待

第一条SQL查询语句

eq_ref: 对于每个来自于前面的表的行组合,从该表中读取一行。这可能是最好的联接类型,除了const类型。它用在一个索引的所有部分被联接使用并且索引是UNIQUEPRIMARY KEY
ref 这个连接类型只有在查询使用了不是惟一或主键的键或者是这些类型的部分(比如,利用最左边前缀)时发生。对于之前的表的每一个行联合,全部记录都将从表中读出。这个类型严重依赖于根据索引匹配的记录多少—越少越好


ref: 对于每个来自于前面的表的行组合,所有有匹配索引值的行将从这张表中读取。如果联接只使用键的最左边的前缀,或如果键不是UNIQUEPRIMARY KEY(换句话说,如果联接不能基于关键字选择单个行的话),则使用ref。如果使用的键仅仅匹配少量行,该联接类型是不错的。ref可以用于使用=<=>操作符的带索引的列。

mysql> EXPLAIN SELECT * FROM student A WHERE studentId = "100001";
+----+-------------+-------+------+---------------------+---------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys       | key     | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------------+---------+---------+-------+------+-------------+
| 1 | SIMPLE      | A     | ref | PRIMARY,i_studentId | PRIMARY | 35      | const | 1    | Using where |
+----+-------------+-------+------+---------------------+---------+---------+-------+------+-------------+
1 row in set (0.00 sec)

mysql> EXPLAIN SELECT * FROM teacher_class A, student_class B where A.classId = B.classId AND B.classId = 2;
+----+-------------+-------+------+--------------------+--------------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys      | key                | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+--------------------+--------------------+---------+-------+------+-------------+
| 1 | SIMPLE      | A     | ref | fk_teacher_classId | fk_teacher_classId | 5       | const | 1    | Using where |
| 1 | SIMPLE      | B     | ref | fk_student_classId | fk_student_classId | 5       | const | 1    | Using where |
+----+-------------+-------+------+--------------------+--------------------+---------+-------+------+-------------+
2 rows in set (0.00 sec)

unique_subquery: 该类型替换了下面形式的IN子查询的ref

mysql> EXPLAIN SELECT * FROM student_class A WHERE A.classId IN (SELECT B.id FROM class B WHERE B.name = "04");
+----+--------------------+-------+-----------------+---------------+---------+---------+------+------+--------------------------+
| id | select_type        | table | type            | possible_keys | key     | key_len | ref | rows | Extra                    |
+----+--------------------+-------+-----------------+---------------+---------+---------+------+------+--------------------------+
| 1 | PRIMARY            | A     | ALL             |               |         |         |      | 3    | Using where              |
| 2 | DEPENDENT SUBQUERY | B     | unique_subquery | PRIMARY,i_id | PRIMARY | 4       | func | 1    | Using index; Using where |
+----+--------------------+-------+-----------------+---------------+---------+---------+------+------+--------------------------+
2 rows in set (0.00 sec)

index_subquery: 该联接类型类似于unique_subquery。可以替换IN子查询,但只适合下列形式的子查询中的非唯一索引:

mysql> EXPLAIN SELECT * FROM student A WHERE A.studentId IN (SELECT B.studentId FROM student_class B WHERE B.classId = 1);
+----+--------------------+-------+----------------+---------------------------------+--------------+---------+------+------+--------------------------+
| id | select_type        | table | type           | possible_keys                   | key          | key_len | ref | rows | Extra                    |
+----+--------------------+-------+----------------+---------------------------------+--------------+---------+------+------+--------------------------+
| 1 | PRIMARY            | A     | ALL            |                                 |              |         |      | 3    | Using where              |
| 2 | DEPENDENT SUBQUERY | B     | index_subquery | fk_studentId,fk_student_classId | fk_studentId | 36      | func | 1    | Using index; Using where |
+----+--------------------+-------+----------------+---------------------------------+--------------+---------+------+------+--------------------------+
2 rows in set (0.00 sec)

index_subquery 与 unique_subquery 不同之处是子查询中的条件形式。一个是以索引为条件的。

range: 这个连接类型使用索引返回一个范围中的行,比如使用>或<查找东西时发生的情况

mysql> EXPLAIN SELECT * FROM student_class A WHERE A.id BETWEEN 1 AND 2;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key     | key_len | ref | rows | Extra       |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE      | A     | range | PRIMARY       | PRIMARY | 4       |      | 1    | Using where |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

index: 该联接类型与ALL相同,除了只有索引树被扫描。这通常比ALL快,因为索引文件通常比数据文件小。当查询只使用作为单索引一部分的列时,MySQL可以使用该联接类型。

mysql> EXPLAIN SELECT count(*) FROM student_class;
+----+-------------+---------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table         | type | possible_keys | key     | key_len | ref | rows | Extra       |
+----+-------------+---------------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE      | student_class | index |               | PRIMARY | 4       |      | 3    | Using index |
+----+-------------+---------------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> EXPLAIN SELECT A.* FROM teacher_class A, class B WHERE A.classId = B.id;
+----+-------------+-------+-------+--------------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys      | key     | key_len | ref | rows | Extra       |
+----+-------------+-------+-------+--------------------+---------+---------+------+------+-------------+
| 1 | SIMPLE      | B     | index | PRIMARY,i_id       | PRIMARY | 4       |      | 2    | Using index |
| 1 | SIMPLE      | A     | ALL   | fk_teacher_classId |         |         |      | 2    | Using where |
+----+-------------+-------+-------+--------------------+---------+---------+------+------+-------------+
2 rows in set (0.00 sec)

ALL: 对于每个来自于先前的表的行组合,进行完整的表扫描。如果表是第一个没标记const的表,这通常不好,并且通常在它情况下差。通常可以增加更多的索引而不要使用ALL,使得行能基于前面的表中的常数值或列值被检索出。

mysql> EXPLAIN SELECT * FROM student A WHERE name = "wangwu";
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE      | A     | ALL |               |      |         |      | 3    | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
原创粉丝点击