Mysql performance tuning (5)

Preface

  In the last article, we introduced the MySQL architecture and related introductions to the storage engine . Next, we will introduce to you the optimization of SQL statements and the specific sql optimization steps. Next we introduce the specific steps to optimize sql.
  In fact, no matter in development or in artificial intelligence, it cannot be separated from the database and used to store the data we need. Since the amount of data was still small in the early days, we paid more attention to the realization of functions when writing SQL statements at the beginning. However, as the project was actually put into use, the amount of data increased exponentially, and many SQL statements started Performance problems gradually appear, and the performance requirements of the project are getting higher and higher. At this time, we should optimize the SQL statement as necessary. Next, we will introduce the specific optimization steps of SQL in Mysql. When we optimize a SQL performance problem, we must analyze the SQL statement itself, and maximize the optimization of the SQL statement as much as possible.

1. Check SQL execution frequency

  When we successfully connect the MySQL client, we can provide server status information through the following commands.

show[session|global] status

  However, we can find from the above command that there is a parameter sessionor globalto display sessionthe calculation results of the level (current connection) and the statistical results of the global level (since the database was last started). If you don't specify it, it will be used by default session. The following command displays the values ​​of all statistical parameters in the current session:

show status like `Com______`;

  The specific results are as follows:

  we can view the status of the sql statement through the following command

show status like `Innodb_rows_%`;

  The specific results are as follows:

  Here we introduce several parameters, where Com_xxx represents the number of executions of each xxx statement. We usually care about the following statistical parameters:

  Here we need to specifically explain the following two parameters:

  • Com_ ***: These parameters will be accumulated for table operations of all storage engines;
  • Innodb_ ***: These parameters are only for the InnoDB storage engine, and the accumulation algorithm is also slightly different.

Second, locate the inefficient execution of SQL

  You can locate SQL statements that have reduced execution efficiency in the following two ways.

  • Slow query log : Use the slow query log to locate the SQL statement that has reduced execution efficiency. When it is started with the -log-slow-queries[=file_name] option, mysqld writes a log file that contains all SQL statements whose execution time exceeds long_query_time seconds.
  • show processlist : The slow query log is recorded after the end of the query, so querying the slow query log cannot locate the problem when the application reflects the execution efficiency problem. You can use the show processlist command to view the current MySQL threads, including thread status, Whether to lock the table, etc., you can view the execution of SQL in real time, and optimize some lock table operations.
show processlist;

  The specific results are as follows:

  Next, we introduce the meaning of each column name in the above table:

  • id column : the "connection_id" assigned by the system when the user logs in to mysql, which can be viewed using the function connection_id()
  • User column : Display the current user. If it is not root, this command will display the sql statement of the user's authority.
  • Host column : Shows which ip and which port the statement was sent from, which can be used to track the user who has the problem statement
  • db column : shows which database the process is currently connected to
  • command column : display the currently connected execution command, generally the value is sleep (sleep), query (query), connect (connect), etc.
  • time column : display the duration of this state, in seconds
  • State column : shows the state of the SQL statement using the current connection, a very important column. State describes a certain state in the execution of a statement. A sql statement, taking a query as an example, may need to be completed by copying to tmp table, sorting result, sending data, etc.
  • Info column : This sql statement is displayed, which is an important basis for judging the problem statement.

Three, explain analysis execution plan

  Through the above steps to query the inefficient SQL statement, you can obtain information about how MySQL executes the select statement through EXPLAINor DESCcommands, including how the tables are connected and the order of the connections during the execution of the select statement. Next, we use the following statement to view the execution plan:

explain select * from tb_item where id = 1;

  The specific results are as follows:

  we achieve it through an example statement

explain select * from tb_item where title = '阿尔卡特(OT-979) 冰川白 联通3G手机3';

  Specific results are as follows:

  in explainthe field, and we introduce specific meaning related to

  the next, we have a detailed case to demonstrate specific optimization sql statements; Let us prepare three tables, namely: t_user, user_role, t_role table , The relationship between them is shown in the figure:

  Next, we create three tables. First, create a t_roletable. The specific commands are as follows:

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;

  Next, we create two tables, namely t_usertable and user_roletable. Since the t_role table has already been built, we will implement it in the form of a diagram for the sake of space:


  Next, we will insert data for the built table. The insert statement is as follows:

1. The id of explain

  The id field is the sequence number of the select query, which is a set of numbers, indicating the order in which the select clause or the operation table is executed in the query. There are three id situations:

  • 1. The same id means that the order of loading the table is from top to bottom:
explain select * from t_role r, t_user u, user_role ur where r.id = ur.role_id and u.id = ur.user_id;

  • 2. The higher the id value is, the higher the priority is, and the earlier it will be executed.
explain select * from t_role where id = (select role_id from user_role where user_id = (select id from t_user where username = 'stu1'));

  • 3. The id is the same, but also different, and exists at the same time. Those with the same id can be considered as a group and executed in order from top to bottom; in all groups, the greater the value of id, the greater the priority, and the earlier it will be executed.
explain select * from t_role r, (select * from user_role ur where ur.`user_id` = '2') a where r.id = a.role_id;

2、explain之select_type

  Indicates the type of select and common values, as shown in the following table:

3、explain之table

  Show which table the data in this row is about

4、explain之type

  Type shows the type of access, which is a more important indicator. The possible values ​​are shown in the figure:

  The results of this table from good to bad are shown in the figure:

NULL > system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

5. Explain the key

  • possible_keys : Show the indexes that may be applied to this table, one or more;
  • key : the index actually used, if it is NULL, the index is not used;
  • key_len : Indicates the number of bytes used in the index. This value is the maximum possible length of the index field, not the actual length used. As long as the accuracy is not lost, the shorter the length, the better.

6、explain之rows

  The number of scan lines.

7, explain the extra

  Other additional execution plan information is displayed in this column.

Four, show profile analysis SQL

  In fact, from the beginning of MySQL version 5.0.37 adds support for show profilesand show profilesupport statements. show profilesIt can help us understand where the time is spent when doing SQL optimization. We can have_profilingsee whether the current MySQL supports profile through parameters;

select @@have_profiling;


  mysql default profiling is turned off, you can turn on profiling at the session level through the set statement;

select @@have_profiling;


  Next we turn on the switch through profiling

set profiling = 1;

  Next, after we passed the profile, we were able to understand the process of SQL execution more clearly. First, we can perform a series of operations, as follows:

show databases;
use db01;
show tables;
select * from tb_item where id < 5;
select count(*) from tb_item;

  After executing the above commands, execute the show profilesinstructions to query the execution time of the SQL statement; the specific process is as follows:

  Next, we use the following commands to view the status and consumption time of each thread during the SQL execution process:

show profile for query query_id;

  What we need to pay attention to here is: the sending data state indicates that the MySQL thread starts to access the data row and returns the result to the client, not just the client. Because in the sending data state, the MySQL thread often needs to do a lot of disk read operations, so it is often the state that takes the longest time in the entire query.

  After obtaining the most time-consuming thread status, MySQL supports further selecting detailed types such as all, cpu, block io, context switch, page faults, etc. to view, what resources MySQL is using too much time. For example, choose to view the time spent on the CPU;

Five, trace analysis optimizer execution plan

  MySQL 5.6 provides a trace of SQL. Through the trace file, we can further understand why the optimizer chooses plan A instead of plan B. We open the trace, set the format to json, and set the maximum memory size that can be used by the trace to avoid a complete display due to the default memory being too small during the parsing process.

set optimizer_trace = "enabled=on", end_markers_in_json = on;
set optimizer_trace_max_men_size = 1000000;

  Execute SQL statement

select * from tb_item where id<4;

  Finally, check information_schema.optimizer_traceto know how MySQL executes SQL:

select * from information_schema.optimizer_trace\G;

  The specific content is as follows:

to sum up

  In the previous article, the architecture and storage engine of our mysql, let us further understand the internal structure and working principle of mysql, and also introduce the difference and specific use of several typical storage engines in mysql. In this article, I introduced you to the optimization of SQL, including viewing SQL execution frequency, locating inefficient SQL, and explain the execution plan of the analysis. 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/109559271
Recommended