MySql optimization analysis of the implementation plan

explain analysis of the implementation plan

Locate the problem through the above steps sql statements in the future we can explain to analyze the select execution plan statement , including the order of how the statement connections and connections

Explain select * from user where id = 1;

This command can display select * from user where id = 1 execution plan of the query

  image.png


Id: select the query sequence number , represents select the order of lookup tables.

Select_type: represents a select type , common values have the SIMPLE ( simple table , i.e., without connecting the table and subqueries ), a PRIMARY ( main query ) that is the outermost subquery, the UNION (Union second query or behind the query ), sUBQUERY (the first sub-query the SELECT ), etc.

Table: Table query

Type: the type of connection performance table is from good to bad : system-> const-> eq_ref-> ref- > ref_or_null-> index_merge-> index_subquery-> range-> index-> all

possible_keys: may be used when the query index

key: the index of actual use

: key_len length of the index field

: rows number of scan lines

extra: and described implementation

 

Below through an example elaborate on:

Create a user table, table roles, user roles correspondence table and insert data

CREATE TABLE `t_role` (

  `id` varchar(32) NOT NULL,

  `role_name` varchar(255) DEFAULT NULL,

  `role_code` varchar(255) DEFAULT NULL,

  `description` varchar(255) DEFAULT NULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `unique_role_name` (`role_name`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

 

CREATE TABLE `t_user` (

  `id` varchar(32) NOT NULL,

  `username` varchar(45) NOT NULL,

  `password` varchar(96) NOT NULL,

  `name` varchar(45) NOT NULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `unique_user_username` (`username`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

 

CREATE TABLE `user_role` (

  `id` int(11) NOT NULL auto_increment ,

  `user_id` varchar(32) DEFAULT NULL,

  `role_id` varchar(32) DEFAULT NULL,

  PRIMARY KEY (`id`),

  KEY `fk_ur_user_id` (`user_id`),

  KEY `fk_ur_role_id` (`role_id`),

  CONSTRAINT `fk_ur_role_id` FOREIGN KEY (`role_id`) REFERENCES `t_role` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,

  CONSTRAINT `fk_ur_user_id` FOREIGN KEY (`user_id`) REFERENCES `t_user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

insert into `t_user` (` id`, `username`,` password`, `name`) values ( '1', 'super', '$ 2a $ 10 $ TJ4TmCdK.X4wv / tCqHW14.w70U3CC33CeVncD3SLmyMXMknstqKRe', ' super administrator ');

insert into `t_user` (` id`, `username`,` password`, `name`) values ( '2', 'admin', '$ 2a $ 10 $ TJ4TmCdK.X4wv / tCqHW14.w70U3CC33CeVncD3SLmyMXMknstqKRe', ' system administrators ');

insert into `t_user` (`id`, `username`, `password`, `name`) values('3','itcast','$2a$10$8qmaHgUFUAmPR5pOuWhYWOr291WJYjHelUlYn07k5ELF8ZCrW0Cui','test02');

insert into `t_user` (`id`, `username`, `password`, `name`) values('4','stu1','$2a$10$pLtt2KDAFpwTWLjNsmTEi.oU1yOZyIn9XkziK/y/spH5rftCpUMZa','学生1');

insert into `t_user` (`id`, `username`, `password`, `name`) values('5','stu2','$2a$10$nxPKkYSez7uz2YQYUnwhR.z57km3yqKn3Hr/p1FR6ZKgc18u.Tvqm','学生2');

insert into `t_user` (`id`, `username`, `password`, `name`) values('6','t1','$2a$10$TJ4TmCdK.X4wv/tCqHW14.w70U3CC33CeVncD3SLmyMXMknstqKRe','老师1');

 

 

 

INSERT INTO `t_role` (`id`, `role_name`, `role_code`, `description`) VALUES('5','学生','student','学生');

INSERT INTO `t_role` (`id`, `role_name`, `role_code`, `description`) VALUES('7','老师','teacher','老师');

INSERT INTO `t_role` (` id`, `role_name`,` role_code`, `description`) VALUES ( '8', ' teaching administrators ', 'teachmanager', ' teaching administrators ');

INSERT INTO `t_role` (` id`, `role_name`,` role_code`, `description`) VALUES ( '9', ' Administrators ', 'admin', ' Administrators ');

INSERT INTO `t_role` (` id`, `role_name`,` role_code`, `description`) VALUES ('10 ',' super administrator ',' super ',' super administrator ');

 

 

INSERT INTO user_role(id,user_id,role_id) VALUES(NULL, '1', '5'),(NULL, '1', '7'),(NULL, '2', '8'),(NULL, '3', '9'),(NULL, '4', '8'),(NULL, '5', '10') ;

 

 

Explain the id:

. 1)     ID in the same order from top to bottom of the table represents a load

explain select * from t_role r, t_user u, user_role ur where r.id = ur.role_id and u.id = ur.user_id ;

image.png

 

Here the first load t_role table reloading t_user, loaded last user_role

2)     id different id higher the value, the higher the priority the earlier it is performed

EXPLAIN SELECT * FROM t_role WHERE id = (SELECT role_id FROM user_role WHERE user_id = (SELECT id FROM t_user WHERE username = 'stu1'))

image.png

Here t_user first to be loaded and then user_role, finally t_role

. 3)     ID have the same or different, exist. id same can be considered as a group, from the order of execution down; in all groups, id larger value, the higher the priority, the first run.

EXPLAIN SELECT * FROM t_role r , (SELECT * FROM user_role ur WHERE ur.`user_id` = '2') a WHERE r.id = a.role_id ;

image.png

 

explain select_type

  It represents select the type of the following values:

SIMPLE: simple query is, do not contain sub-queries and union

PRIMARY: If a query contains subqueries that the parent query with the flag

SUBQUERY: express subqueries

DERIVED: representation from from the temporary table query query results in the formation of the child out of the query

UNION: joint query UNION after the select statement

UNIONRESULT: by UNION joint statement

explain table
Which indicates that the query is for tables of
explain type
type represents the type of access is a very important indicator of possible values :
NULL : MySQL does not access any tables and indexes, direct return results
For example : the SELECT the NOW ();
System: table has only one row, generally does not appear
Const: represented by the index query and returns only one record, where the index refers to the primary key index and a unique index.
Eq_ref: multi-table associated with the query, and the query is only one out of the data
Ref: According to the non-uniqueness of the index query, check out the results of a number of
Range: Range Scan, WHERE after between,>, <, = , etc.
Index: represents the entire index tree traversal
All: will traverse the whole table
Query efficiency in descending order are : System> const> eq_ref> ref> the Range> index> ALL
In general we need to let it reach the range or higher
explain the key
possible_keys: possible application of the index, one or more
key: the actual use of the index, if the index is not used is empty
key_len: number of bytes in the index, the maximum possible length of the index field, a length as short as possible
explain rows
The number of scanning lines
explain the extra
Other additional information indicating execution
filesort a using : representation of the field is not indexed sort.
the Temporary a using : using a temporary table to hold intermediate results, common language group by statement
using index: represents the select operation uses an index


Guess you like

Origin blog.51cto.com/11583017/2471841