Performance test optimization skills-positioning MySQL slow query


introduction

The most important part of performance testing is performance optimization. When the response time is relatively long, or the tps cannot go up, it may be because of SQL slow query.

Slow account opening query log

mysql> set global slow_query_log = on;
Query OK, 0 rows affected (0.01 sec)

Set the slow query time limit (as long as the query time is greater than this value, it will be recorded in the slow query log, unit: second)

mysql> show global variables like "slow_query_log_file";

Determine the slow query log path

tail -n5 E:\soft\mysql-5.7.23-winx64\data\liangf-slow.log

、

Determine the slow query log file name

tail -n5 E:\soft\mysql-5.7.23-winx64\data\liangf-slow.log

View log command

tail -n5 E:\soft\mysql-5.7.23-winx64\data\liangf-slow.log

Locate slow queries through show processlist

Sometimes the slow query is being executed, which has caused the database load to be too high, and because the slow query has not been executed yet, no statement can be seen in the slow query log. At this time, you can use the show processlist command to determine the slow query being executed. show processlist shows which threads are running. If you have PROCESS permission, you can see all threads. Otherwise, you can only see the threads of the current session.

Knowledge expansion: If the FULL keyword is not used, only the first 100 characters of each sentence are displayed in the info field. If you want to see the entire content of the sentence, you can use the full modification (show full processlist).

Here is an explanation of the key parameters of the above results:

Time: indicates the execution time

Info: Represents SQL statements

Here we can judge whether it is slow SQL by its execution time (Time).

EXLPAIN analysis slow query

Analyzing SQL execution efficiency is an important means to optimize SQL. Through the two methods mentioned above, after locating the slow query statement, we will begin to analyze the SQL execution efficiency. We can analyze it through diagnostic tools such as explain, show profile, and trace. Slow query.

Explain can get the execution plan of the SQL statement in MySQL, such as whether the statement uses the associated query, whether the index is used, the number of scanned rows, etc. Can help us choose better indexes and write better SQL. How to use: Just add explain in front of the query statement and run it.

Create a test table and insert some data for testing:

#create table
CREATE TABLE `test_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
`d` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_a` (`a`),
KEY `idx_b_c` (`b`,`c`)
) ENGINE=InnoDB CHARSET=utf8mb4;

# insert data proc
DELIMITER //
CREATE PROCEDURE insert_test_data()
BEGIN
 DECLARE i INT;
 SET i=1;
 WHILE(i<=160000) DO
 INSERT INTO test_table (a,b,c,d) VALUES (i,i,i,i);
 SET i=i+1;
END WHILE;
END //
# exec proc
CALL insert_test_data();

We created 3 indexes: PRIMARY KEY ( id), clustered index KEY idx_a( a), non-clustered index KEY idx_b_c( b, c) non-clustered index d column did not create an index

Conclusion

In many cases, if you don't have database permissions, it doesn't matter. You can ask for help from operation and maintenance or development.

I am a test lady in the workplace! Just finished the test tutorial, I will share it again. Testers who are interested in python automated testing, web automation, interface automation, mobile terminal automation, interview experience exchange, etc., can follow the WeChat public account:[Sad Spicy Strips], Obtain the interview information of the software test engineer from Dachang! My learning exchange group: 902061117 group has technical experts to communicate and share together~

If the article is helpful to you, please reach out to make a fortune and give me a like. Thank you for your support. Your likes are my motivation for continuous updating.

Recommended reading:

What kind of person is suitable for software testing?

Talking about starting from a small company to a big factory, what did I do right?

Want to switch to software testing? Come and see if you are suitable

From self-study to work in software testing, how should software testing learning be carried out?

How to write a software test engineer resume project experience?-1,000 software test engineer resume templates (real resume) that have been successfully recruited

Guess you like

Origin blog.csdn.net/weixin_50271247/article/details/115248439
Recommended