mysql server performance analysis

Preface

I recently read the chapter on server performance analysis of high-performance mysql. After reading the records, I sorted out what I learned.

1. What is performance optimization

The book puts forward a simple explanation that under the normal operation of the service, the response time of the service is reduced (the response time is replaced by rt ​​later).
So since we want to reduce the response time of the service, that is to say, the rt after optimization is smaller than the previous rt. The premise here is that
we know what the previous rt is, then optimize, and then compare the new rt with the previous one . If the new rt is relatively small, then a successful optimization is performed.

Here is another key point, how to measure or measure server performance and response time?
Let's take it step by step. In order to reduce the response time, we now need to measure the response time, and then we have to analyze the current response time to figure out which step is more time-consuming and why it is so slow, and then prescribe the right medicine to reduce a certain link Time-consuming, and then achieve the optimized result.

2. Understand performance optimization

Generally speaking, performance optimization in mysql refers to select, and the response time can actually be subdivided into two parts:
execution time and waiting time. When performing performance analysis, it is necessary to understand which part of the problem is the main one. If it is mainly the waiting time, such as 90% of the execution time, 10% of the execution time, then the waiting time may be the I/O problem first.
Several rules for performance optimization:

2.1 Queries worth optimizing:

1. If the proportion of a response time does not exceed 5%, then it is not worth optimizing. No matter how hard you work, the benefit will not exceed 5%
. 2. If the cost of optimization is greater than the benefit, then it is not worth optimizing. It takes a lot of time to optimize. Improve an already fast query, then the time spent is not worth it, it can be said to be reverse optimization, wasted time to do some useless work

2.2 Abnormal situation:

Some low-frequency queries may not be many, but they take a long time and directly affect the user experience. Such queries also need to be found and optimized.

2.3 Unknown unknown

The time measured by the measurement tool may not be exactly the same as the execution time of the actual application, and there may be a difference. For example, the measurement tool measured 9.6 seconds, the actual program printing time may be
11 seconds, the difference of 1400 milliseconds was not measured, and some subtasks of the program were not measured. This may lead to inaccurate troubleshooting and affect optimization. The progress.

2.4 Hidden details

Here is an example for better understanding. There are 10,000 queries, 1-2 queries are very slow, and the call frequency is not much, and the others are very fast. On average, the response time of this 1-2 query is reduced by
If you average it, you don't see anything abnormal. This kind of query still needs to be optimized. At a certain time, because there are too many calls, the server may be directly suspended or hanged.

3. Dissecting the application

In this book, php is taken as an example to describe some specific performance analysis tools. Each language has different SQL performance analysis tools, so I won't go into details here.

4. Anatomy of mysql query

4.1 Server load query

4.1.1 The problem of the server can be found through the slow query log of mysql. It is recommended to use pt-query-digest
4.1.2 to analyze the query log by capturing the tcp network packet, and then analyzing the communication protocol between the mysql client and the server for accurate analysis.

4.2 Single performance query

1. The use of show profile
is disabled by default, but it can be dynamically turned on by modifying session-level variables.

mysql>    SET profiling = 1;

After opening, all sentences, time consumed and related parameters will be measured and saved.
Official document address

2. Use show status
official document address

show status can view various information in the session, and can also see global variables. If you want to query global variables, use show globals status.
The basic usage is as follows:

show status like '变量名';

Here are some commonly used methods:

--查看MySQL本次启动后的运行时间(单位:秒)

show status like 'uptime';

--查看select语句的执行数

show [global] status like 'com_select';

--查看insert语句的执行数

show [global] status like 'com_insert';

--查看update语句的执行数

show [global] status like 'com_update';

--查看delete语句的执行数

show [global] status like 'com_delete';

--查看试图连接到MySQL(不管是否连接成功)的连接数

show status like 'connections';

--查看线程缓存内的线程的数量。

show status like 'threads_cached';

--查看当前打开的连接的数量。

show status like 'threads_connected';

--查看当前打开的连接的数量。

show status like 'threads_connected';

--查看创建用来处理连接的线程数。如果Threads_created较大,你可能要增加thread_cache_size值。

show status like 'threads_created';

--查看激活的(非睡眠状态)线程数。

show status like 'threads_running';

--查看立即获得的表的锁的次数。

show status like 'table_locks_immediate';

--查看不能立即获得的表的锁的次数。如果该值较高,并且有性能问题,你应首先优化查询,然后拆分表或使用复制。

show status like 'table_locks_waited';

--查看创建时间超过slow_launch_time秒的线程数。

show status like 'slow_launch_threads';

--查看查询时间超过long_query_time秒的查询的个数。

show status like 'slow_queries';

3. Use the
official document address of the
slow query log. By default, the slow query log is turned off. The variable slow_query_log is the slow query log, which can be turned on by setting the variable.

查询慢查询日志:
mysql> show variables  like '%slow_query_log%';
+---------------------+-----------------------------------------------+
| Variable_name       | Value                                         |
+---------------------+-----------------------------------------------+
| slow_query_log      | OFF                                           |
| slow_query_log_file | /xxx/mysql/DB-Server-slow.log |
+---------------------+-----------------------------------------------+

设置慢查询日志
mysql> set global slow_query_log=1;

4. Use the PERFORMANCE_SCHEMA
official document address.
Here, take mysql5.6 as an example. PERFORMANCE_SCHEMA is a table that records the performance of the mysql server. The default is off, you need to enable the settings to be useful.

如下开启: 
    [mysqld]
    performance_schema=ON
查看下是否开启:
mysql>show variables like 'performance_schema';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| performance_schema | ON    |
+--------------------+-------+

5. Other diagnostic tools

5.1 USER_STATISTICS

Percona Server和MariaDB是支持的,官方的mysql我试了下好像没有找到这个。据说官方分支的mysql是使用的performance
schema表。
 这个工具可以查看到什么表,什么索引使用最频繁或者最频繁等信息。
 命令:
```
mysql> show tables from information_schema like "%_STATISTICS";
```

5.2 stace

stace can view and print out the system calls, which are linux commands, not mysql's own.

使用方式: strace -p pid or strace command

Common sentences:

1.通用示例:  

#strace -o output.txt -T -tt -e trace=all -p 28979

跟踪28979进程的所有系统调用(-e trace=all),并统计系统调用的花费时间,以及开始时间(并以可视化的时分秒格式显示),最后将记录结果存在output.txt文件里面



2.统计httpd进程的耗时时间:

#strace -c -p $(pgrep -n httpd)

按crtl+c,将显示从开始到结束的时间调用



3.跟踪最占cpu的httpd的一个进程,并将信息输出到文件

#strace -t -f  -o httpd-strace  -p $(top -b -n1 | grep "httpd" | head -1 | awk '{print $1}')

to sum up:

1. The most direct and effective way of performance measurement is based on the response time
. 2. It is best to start measuring performance from the application.
3. Response time is divided into: execution time and waiting time
4. You must first understand whether the execution time or the waiting time is too long Affected performance
5. Less than 5% of the response time does not need to consider optimization, it is necessary to spend effort on the knife edge, when the optimization cost is higher than the profit, it is not worth to optimize
6. Any measurement tool may have some tasks or places where measurement is not However, hidden details may be the key, and it will be more accurate to combine analysis in multiple ways

Guess you like

Origin blog.csdn.net/sc9018181134/article/details/106183759