Mysql performance tuning (6)

Preface

  First of all, I would like to say sorry to everyone. Because I have been preparing for the National Grid Exam in the past month, time is tight and the task is heavy, so there has been no time to update. I participated in the interview with State Grid yesterday and is now waiting for the result. Recently I will update as much as possible , but I will still ensure the quality of the article . I will share the knowledge I have learned with you in the form of a blog, so that everyone can read my articles and gain a lot of rewards.
  In the previous article, we introduced the specific steps of optimizing SQL statements , including checking the execution frequency of SQL, locating low-efficiency execution of SQL, explain analysis of the execution plan, analyzing SQL through show profile, and finally analyzing the execution plan of the optimizer through trace. The following article will introduce you to the specific use of indexes in mysql.
  In the previous article we also introduced index of basic concepts and basic use of the index. In fact, indexing is one of the most commonly used and most important technical means for database optimization. Indexing can usually help users solve most of the performance optimization problems of MySQL. Next, we will introduce to you the statement that verifies the index in mysql to improve query efficiency.

1. Verify the index to improve query efficiency

  We perform ID query on a big data table, and the data in this table has reached 3 million records.

select * from tb_item where id = 1999\G;

  When we look at the results of its execution,

  we can clearly see that its query speed is very fast, close to 0s. The main reason is that id is the primary key and an index is added by default . We use the following statement to verify the index of our primary key;

explain select * from tb_item where id = 1999\G;

  We look at the results of its execution.

  If we query according to its name, then look at the execution plan of this statement.

explain select * from tb_item where title = 'iphonex 移动3G 32G941'\G;

  When we look at the results of its execution,

  we can clearly see that it takes a certain amount of time to query on columns that are not indexed. We build an index for the title class; the statement is as follows:

create index idx_item_title on tb_item(title);

  After we view the results of its execution and

  create the index, we query the title again:

select * from tb_item where title = 'iphonex 移动4G 64G944'\G;

  We check the results of its execution.

  In order for the content we introduce to proceed smoothly, we will create a tb_sellertable for everyone and insert some data as the table for our experiment. Our storage engine still uses the default innodb. The specific table creation statement is as follows:

create table `tb_seller`(
	`sellerid` varchar (100),
	`name` varchar(100),
	`nickname` varchar (50),
	`password` varchar (60),
	`status` varchar (1),
	`address` varchar (100),
	`createtime` datetime,
	primary key(`sellerid`)
) engine=innodb default charset=utf8mb4;

  Next, we tb_sellerinsert data for the table, the specific statement is as follows:

insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`)
values ('xiaomi', '小米科技', '小米官方旗舰店','e10adc3949ba59abbe56e057f20f883e', '1', '西安市', '2088-01-01 12:00:00');
insert into `tb_seller` (`sellerid`, `name`, `nickname`, `password`, `status`, `address`, `createtime`)
values ('yijia', '宜家家居', '宜家家居旗舰店','e10adc3949ba59abbe56e057f20f883e', '1', '北京市', '2088-01-01 12:00:00');

  The following data is consistent with the above code. In order to save time, we will give it to you in the form of pictures. Specific data inserted as follows:

  Next, we name, status, addressthree fields to create the index, in particular to achieve the following statement:

create index idx_seller_name_sta_addr on tb_seller(name, status, address);

  Next, I will introduce several ways to avoid index failure.

2. Avoid index failure

  1), full value matching , specify specific values ​​for all columns in the index. In this case, the index takes effect and the execution efficiency is higher.

explain select * from tb_seller where name = '小米科技' and status = '1' and address = '北京市'\G; 

  The specific implementation effects are as follows:

  2), the leftmost prefix rule . If you index multiple columns, follow the leftmost prefix rule. It means that the query starts from the leftmost front column of the index and does not skip the columns in the index . What we need to pay attention to is that the rule of matching the leftmost prefix is ​​indexed.

explain select * from tb_seller where name = '小米科技'; 

  The specific implementation effects are as follows:

  3) The column on the right of the range query cannot use the index .

explain select * from tb_seller where name = '小米科技' and status = '1' and address = '北京市'; 

  The specific implementation effects are as follows:

explain select * from tb_seller where name = '小米科技' and status > '1' and address = '北京市'; 

  The specific implementation effect is as follows:

  According to the previous two fields, the status query is indexed. But the last condition address is useless index.
  4) Do not perform calculation operations on the index column, the index will be invalid.

select * from tb_seller where substring(name, 3,2) = '科技';

  The specific implementation effects are as follows:

   5) The string does not add single quotation marks, causing the index to become invalid.

explain select * from tb_seller where name = '科技' and status = '0';

  The specific implementation effects are as follows:

explain select * from tb_seller where name = '科技' and status = 0;

  The specific implementation effects are as follows:

   6), try to use covering index, avoid select. Try to use a covering index (query that only accesses the index (the index column completely contains the query column)), and reduce select *.

explain select * from tb_seller where name = '科技' and status = '0' and address = '西安市';

  The specific implementation effects are as follows:

explain select name from tb_seller where name = '科技' and status = '0' and address = '西安市';

  The specific implementation effects are as follows:

explain select name,status from tb_seller where name = '科技' and status = '0' and address = '西安市';

  The specific implementation effects are as follows:

explain select name,status, address from tb_seller where name = '科技' and status = '0' and address = '西安市';

  The specific implementation effect is as follows:

  if the query column exceeds the index column, performance will also be reduced.

explain select status, address, password from tb_seller where name = '科技' and status = '0' and address = '西安市';

  The specific implementation of the results is as follows:

  in the implementation of the pot, we can see a different index names in the Extra column, some Using index, , using where, using index condition, using index; using wherewe are going to explain to you the meaning of each of these four types of indexes.

  using index : when using a covering index, there will be
  using where : in the case of searching and using an index, you need to go back to the table to query the required data
  using index condition : search using an index, but you need to go back to the table to query the data
  using index; using where : The search uses an index, but the required data can be found in the index column, so there is no need to go back to the table to query the data.

   7). Separate the condition with or. If the column in the condition before or has an index, but there is no index in the following column, then the index involved will not be used.
  Let's use an example to explain the above principle for everyone. The name field is an index column, and createtime is not an index column. The middle is or is connected without indexing;

explain select * from tb_seller where name = '黑马程序员' and createtime = '2088-01-01 12:00:00'\G;

  The specific implementation effects are as follows:

explain select * from tb_seller where name = '黑马程序员' or createtime = '2088-01-01 12:00:00'\G;

  The specific execution effect is as follows:

   8) Like fuzzy query starting with %, the index is invalid. If only the tail is fuzzy matching, the index will not be invalidated. If the head is fuzzy matching, the index is invalid.

explain select * from tb_seller where name like '黑马程序员%';

  The specific implementation effects are as follows:

   9) If MySQL evaluates that the use of indexes is slower than that of the full table, then indexes are not used.

explain select * from tb_seller where address = '北京市';

  The specific implementation effects are as follows:

create index idx_address on tb_seller(address);

  The specific implementation effects are as follows:

explain select * from tb_seller where address = '北京市';

  The specific implementation effects are as follows:

explain select * from tb_seller where address = '西安市';

  The specific implementation effects are as follows:

   10), is NULL, is NOT NULL sometimes the index fails.

explain select * from tb_seller where name is null;

  The specific implementation effects are as follows:

explain select * from tb_seller where name is not null;

  The specific implementation effects are as follows:

explain select * from t_user where name is null;

  The specific implementation effects are as follows:

explain select * from t_user where name is not null;

  The specific implementation effects are as follows:

   11) In takes index, not in index is invalid.

explain select * from tb_seller where sellerid in ('oppo', 'xiaomi', 'sina');

  The specific implementation effects are as follows:

explain select * from tb_seller where sellerid not in ('oppo', 'xiaomi', 'sina');

  The specific implementation effects are as follows:

   12), single-column index and composite index. Try to use composite indexes, and less single-column indexes. Create a composite index.

create index idx_name_sta_address on tb_seller(name, status, address);

   This statement is equivalent to creating three indexes:
  name
  name + status
  name + status + address

  Create a single-column index

create index idx_seller_name on tb_seller(name);
create index idx_seller_name on tb_seller(status);
create index idx_seller_name on tb_seller(address);

  The database will choose an optimal index to use, and will not use all indexes.

3. Check index usage

show status like 'Handler_read%';
show global status like 'Handler_read%';

  The specific execution effect is as follows:

  From the perspective of the execution effect, there are some variable names in the executed table. Next, we will introduce the specific meaning of these variable names.

  Handler_read_first : The number of times the first entry in the index has been read. If it is higher, it means that the server is performing a large number of full index scans ( the lower the value, the better )
  Handler_read_key : If the index is working, this value represents the number of times a row is read by the index value, if the value is lower. Indicates that the performance improvement of the index is not high, because the index is not used frequently ( the higher the value, the better ).
  Handler_read_next : The number of requests to read the next line in key order. If you use range constraints or if you perform an index scan to query indexed columns, the value increases.
  Handler_read_prev : The number of requests to read the previous line in key order. This reading method is mainly optimized for ORDER BY... DESC.
  Handler_read_rnd : The number of requests to read a line according to a fixed position. If you are executing a large number of queries and need to sort the results, this value is higher. You may use a lot of queries that require MySql to scan the entire table or your connection may not use keys correctly. This value is relatively high, which means that the operating efficiency is low, and an index should be established to remedy it.
  Handler_read_rnd_next : The number of requests to read the next line in the data file. If you are performing a large number of table scans, this value is higher. It usually means that your table index is incorrect or the query written does not make use of the index.

to sum up

  In the previous article, we optimized the specific steps of sql statement , including checking the execution frequency of sql, locating inefficient execution of sql, explain analysis of execution plan, analysis of sql through show profile, and finally analysis of optimizer execution plan through trace and other related knowledge. This article introduces you to the specific use of indexes in mysql. The next article will introduce you to the optimization of sql. Therefore, mysql is a very important skill. Almost every job in the computer needs a mysq skill. Therefore, we need special mastery. Life is endless and struggle is endless. We work hard every day, study hard, constantly improve our abilities, and believe that we will learn something. Come on! ! !

Guess you like

Origin blog.csdn.net/Oliverfly1/article/details/111088289