Home Database Mysql Tutorial Detailed introduction to MySQL's pt-query-digest analysis of slow query logs

Detailed introduction to MySQL's pt-query-digest analysis of slow query logs

Mar 20, 2017 pm 02:15 PM

This article mainly introduces the relevant information about MySQL slow query pt-query-digest analysis of slow query logs. The introduction in the article is very detailed and has certain reference value for everyone. Friends who need it Let’s take a look together below.

1. Introduction

pt-query-digest is a tool used to analyze mysql slow queries. It Binlog, general log, slowlog can be analyzed, or the MySQL protocol data captured through SHOWPROCESSLIST or tcpdump can be analyzed. The analysis results can be output to a file. The analysis process is to first parameterize the conditions of the query statement, and then perform group statistics on the parameterized queries to calculate the execution time, number, proportion, etc. of each query. You can use the analysis As a result, problems are identified and optimized.

2. Installationpt-query-digest

##2.perl module

yum install -y perl-CPAN perl-Time-HiRes
Copy after login

3. Installation steps


Method 1: rpm installation

cd /usr/local/src
wget percona.com/get/percona-toolkit.rpm
yum install -y percona-toolkit.rpm
Copy after login

The tool installation directory is:/usr/bin

Method 2: Source code installation

cd /usr/local/src
wget percona.com/get/percona-toolkit.tar.gz
tar zxf percona-toolkit.tar.gz
cd percona-toolkit-2.2.19
perl Makefile.PL PREFIX=/usr/local/percona-toolkit
make && make install
Copy after login

The tool installation directory is: /usr/local/percona-toolkit/bin

4. Introduction to the usage of each tool


(1) Slow query log analysis statistics

pt-query-digest /usr/local/mysql/data/slow.log
Copy after login

(2) Server summary

pt-summary
Copy after login

(3) Server disk monitoring

pt-diskstats
Copy after login

(4)Mysql service status summary

pt-mysql-summary -- --user=root --password=root
Copy after login

3. pt-query-digest syntax and important options

    ##pt-query-digest [OPTIONS] [FILES] [DSN]
  1. --create-review-table When using the --review parameter to output the analysis results to a table, it will be automatically created if there is no table.
  2. --create-history-table When using the --history parameter to output the analysis results to a table, it will be automatically created if there is no table.
  3. --filter Match and filter the input slow query according to the specified
  4. string

    and then analyze it

  5. --limit Limits the percentage or quantity of output results. The default value is 20, which means outputting the 20 slowest statements. If it is 50%, it will be sorted from large to small by the total response time, and the output will be cut off when the total reaches 50%.
  6. --host mysql server address
  7. --user mysql username
  8. -- password mysql user password
  9. --history Save the analysis results to the table. The analysis results are more detailed. The next time you use --history, if the same statement exists and the query is If the time interval is different from that in the history table, it will be recorded in the data table. You can compare the historical changes of a certain type of query by querying the same CHECKSUM.
  10. --review Save the analysis results to the table. This analysis only parameterizes the query conditions. One type of query is for one record, which is relatively simple. When --review is used next time, if the same statement analysis exists, it will not be recorded in the data table.
  11. --output analysis result output type, the value can be report (standard analysis report), slowlog (Mysql slow log),
  12. json

    , json-anon, general Use report for easier reading.

  13. --since the time from which to start analysis, the value is a string, which can be a specified time point in the format of "yyyy-mm-dd [hh:mm:ss]" , or it can be a simple time value: s (seconds), h (hours), m (minutes), d (days). For example, 12h means that statistics started 12 hours ago.
  14. --until deadline, combined with --since can analyze slow queries within a period of time.

4. Analysis of pt-query-digest output results

Part 1: Overall Statistical results


Overall: How many queries are there in total


Time range: The time range of query execution


unique: Unique Number of queries, that is, how many different queries there are after parameterizing the query conditions


total: total min: minimum max: maximum avg: average


95 %: Arrange all values ​​from small to large, the number located at the 95th percentile, this number generally has the most reference value


median: Median, arrange all values ​​from small to large, the position is at The number in the middle

# 该工具执行日志分析的用户时间,系统时间,物理内存占用大小,虚拟内存占用大小
# 340ms user time, 140ms system time, 23.99M rss, 203.11M vsz
# 工具执行时间
# Current date: Fri Nov 25 02:37:18 2016
# 运行分析工具的主机名
# Hostname: localhost.localdomain
# 被分析的文件名
# Files: slow.log
# 语句总数量,唯一的语句数量,QPS,并发数
# Overall: 2 total, 2 unique, 0.01 QPS, 0.01x concurrency 
# 日志记录的时间范围
# Time range: 2016-11-22 06:06:18 to 06:11:40
# 属性    总计  最小 最大 平均 95% 标准 中等
# Attribute   total  min  max  avg  95% stddev median
# ============  ======= ======= ======= ======= ======= ======= =======
# 语句执行时间
# Exec time    3s 640ms  2s  1s  2s 999ms  1s
# 锁占用时间
# Lock time   1ms  0  1ms 723us  1ms  1ms 723us
# 发送到客户端的行数
# Rows sent    5  1  4 2.50  4 2.12 2.50
# select语句扫描行数
# Rows examine  186.17k  0 186.17k 93.09k 186.17k 131.64k 93.09k
# 查询的字符数
# Query size   455  15  440 227.50  440 300.52 227.50
Copy after login

Part 2: Query group statistical results


Rank: Ranking of all statements, by default arranged in descending order by query time, passed-- order-by specifies


Query ID: ID of the statement, (remove excess spaces and text characters, calculate

hash

value)
Response: Total Response time


time: The total time proportion of this query in this analysis


calls: Number of executions, that is, how many such queries are there in this analysis Type of query statement


R/Call: Average response time of each execution


V/M: Response time Variance-to-mean ratio

Item:查询对象

# Profile
# Rank Query ID   Response time Calls R/Call V/M Item
# ==== ================== ============= ===== ====== ===== ===============
# 1 0xF9A57DD5A41825CA 2.0529 76.2%  1 2.0529 0.00 SELECT
# 2 0x4194D8F83F4F9365 0.6401 23.8%  1 0.6401 0.00 SELECT wx_member_base
Copy after login

第三部分:每一种查询的详细统计结果

由下面查询的详细统计结果,最上面的表格列出了执行次数、最大、最小、平均、95%等各项目的统计。

ID:查询的ID号,和上图的Query ID对应

Databases:数据库名

Users:各个用户执行的次数(占比)

Query_time distribution :查询时间分布, 长短体现区间占比,本例中1s-10s之间查询数量是10s以上的两倍。

Tables:查询中涉及到的表

Explain:SQL语句

# Query 1: 0 QPS, 0x concurrency, ID 0xF9A57DD5A41825CA at byte 802 
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: all events occurred at 2016-11-22 06:11:40
# Attribute pct total  min  max  avg  95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count   50  1
# Exec time  76  2s  2s  2s  2s  2s  0  2s
# Lock time  0  0  0  0  0  0  0  0
# Rows sent  20  1  1  1  1  1  0  1
# Rows examine 0  0  0  0  0  0  0  0
# Query size  3  15  15  15  15  15  0  15
# String:
# Databases test
# Hosts  192.168.8.1
# Users  mysql
# Query_time distribution
# 1us
# 10us
# 100us
# 1ms
# 10ms
# 100ms
# 1s ################################################################
# 10s+
# EXPLAIN /*!50100 PARTITIONS*/
select sleep(2)\G
Copy after login

五、用法示例

1.直接分析慢查询文件:

pt-query-digest slow.log > slow_report.log
Copy after login

2.分析最近12小时内的查询:

pt-query-digest --since=12h slow.log > slow_report2.log
Copy after login

3.分析指定时间范围内的查询:

pt-query-digest slow.log --since '2017-01-07 09:30:00' --until '2017-01-07 10:00:00'> > slow_report3.log
Copy after login

4.分析指含有select语句的慢查询

pt-query-digest --filter '$event->{fingerprint} =~ m/^select/i' slow.log> slow_report4.log
Copy after login

5.针对某个用户的慢查询

pt-query-digest --filter '($event->{user} || "") =~ m/^root/i' slow.log> slow_report5.log
Copy after login

6.查询所有所有的全表扫描或full join的慢查询

pt-query-digest --filter '(($event->{Full_scan} || "") eq "yes") ||(($event->{Full_join} || "") eq "yes")' slow.log> slow_report6.log
Copy after login

7.把查询保存到query_review表

pt-query-digest --user=root –password=abc123 --review h=localhost,D=test,t=query_review--create-review-table slow.log
Copy after login

8.把查询保存到query_history表

pt-query-digest --user=root –password=abc123 --review h=localhost,D=test,t=query_history--create-review-table slow.log_0001
pt-query-digest --user=root –password=abc123 --review h=localhost,D=test,t=query_history--create-review-table slow.log_0002
Copy after login

9.通过tcpdump抓取mysql的tcp协议数据,然后再分析

tcpdump -s 65535 -x -nn -q -tttt -i any -c 1000 port 3306 > mysql.tcp.txt
pt-query-digest --type tcpdump mysql.tcp.txt> slow_report9.log
Copy after login

10.分析binlog

mysqlbinlog mysql-bin.000093 > mysql-bin000093.sql
pt-query-digest --type=binlog mysql-bin000093.sql > slow_report10.log
Copy after login

11.分析general log

pt-query-digest --type=genlog localhost.log > slow_report11.log
Copy after login

总结

The above is the detailed content of Detailed introduction to MySQL's pt-query-digest analysis of slow query logs. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

See all articles