The previous articles describes the underlying data structure and mysql mysql optimization artifact explain. Some friends said backstage Xiaoqiang only introduce the concept of normal use or look ignorant, urged a little strong to combat sql optimization, two days over the weekend to organize and summarize, sql optimization combat freshly baked, we usually study and work experience 90% of the sql optimization will be introduced to the mind is too long and is divided into three articles of Kazakhstan.
CREATE TABLE `employees` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(24) NOT NULL DEFAULT '' COMMENT '姓名',
`age` int(20) NOT NULL DEFAULT '0' COMMENT '年龄',
`position` varchar(20) NOT NULL DEFAULT '' COMMENT '职位',
`hire_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '入职时间',
PRIMARY KEY (`id`),
KEY `idx_name_age_position` (`name`,`age`,`position`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='员工表';
insert into employees(name,age,position,hire_time) values('LiLei', 22, 'manager', NOW())
insert into employees(name,age,position,hire_time) values('HanMeimei', 23, 'dev', NOW())
insert into employees(name,age,position,hire_time) values('Lucy', 23, 'dev', NOW())
Full value match
Index field type is varchar (n): 2-byte string length storage, if it is utf-8, then the length is 3n + 2
EXPLAIN select * from employees where name='LiLei';
EXPLAIN select * from employees where name='LiLei' AND age = 22;
EXPLAIN select * from employees where name='LiLei' AND age = 22 AND position = 'manager';
The most left-prefix rule
If the index is a multi-column, to the most leftmost prefix rule. It refers to a query from the leftmost in the forefront of the beginning and not skip index column in the index. The following two sql According to the most left-prefix rule, will not take the index.
EXPLAIN select * from employees where age = 22 AND position='manager';
EXPLAIN select * from employees where position ='manager';
Failure Index
Do not do anything (computing, functions, type conversion) column on the index will lead to failure while the index turned full table scan.
EXPLAIN select * from employees where name='LiLei';
EXPLAIN select * from employees where left(name, 3)='LiLei';
Hire_time to a general increase in the index:
alter table `employees` ADD INDEX `idx_hire_time`(`hire_time`) USING BTREE;
EXPLAIN select * from employees where date(hire_time) = '2019-08-25';
Restore the original state index
ALTER TABLE `employees` DROP INDEX `idx_hire_time`;
Storage engine can not use the index in the right column of the range conditions
-- EXPLAIN SELECT * FROM employees WHERE name ='LiLei' AND age=22 AND position ='manager';
EXPLAIN SELECT * FROM employees WHERE name ='LiLei' AND age>22 AND position ='manager';
Key_len see this index length is 78, that is, only to the first two fields name and age, postition not to use the index.
Covering index
Try to use a covering index (an index only access to query (the index column contains a query column)), reduce selelct * statement.
EXPLAIN SELECT name,age,position FROM employees WHERE name ='LiLei' AND age=22 AND position ='manager';
Conditional
mysql is not equal in use (! = or <>) can not be used when the index will lead to a full table scan
EXPLAIN SELECT * FROM employees WHERE name !='LiLei' ;
Analyzing null
is null, is not null can not use the index
EXPLAIN SELECT * FROM employees WHERE name is null;
like
Wildcard like to begin ( '$ abc') mysql index table scans failure becomes perfected
EXPLAIN SELECT * FROM employees WHERE name LIKE '%Lei';
String failure without single quotation marks index
EXPLAIN SELECT * FROM employees WHERE name ='1000';
EXPLAIN SELECT * FROM employees WHERE name =1000;
String without single quotation marks, mysql uses cust underlying function to convert it to a string, then indexes failure.
or & in less use
Or less or in, use it query, mysql not necessarily use the index, mysql internal optimizer will use an index based on an overall assessment of whether a number of factors index ratio, table size.
EXPLAIN SELECT * FROM employees WHERE name ='LiLei' or name='HanMeimei';
Range query optimization
Add a single index value for the age
ALTER TABLE `employees`ADD INDEX `idx_age`(`age`) USING BTREE;
EXPLAIN select * from employees where age > 1 and age <= 2000;
There is no reason to go indexes: mysql internal optimizer will use an index based on an overall assessment of whether a number of factors retrieve ratio, table size.
This example does not take the index may be due to a single large amount of data query optimizer chose not lead to taking the index.
Optimization method: a large range may be split into a plurality of small ranges.
I'm not concerned about the number of the public?
- Sweep end the two-dimensional code number [of public concern Xiaoqiang advanced road] can receive as follows:
- Learning materials: 1T Video Tutorial: front and rear end covers Javaweb instructional videos, machine learning / AI instructional videos, Linux system tutorial videos, IELTS video tutorials;
- More than 100 books: containing C / C ++, Java, Python programming language must see three books, LeetCode explanations Daquan;
- Software tools: Most software includes almost you might use in programming the road;
- Project Source: 20 JavaWeb source projects.