


What is the difference between slow commit in MySQL slow query and slow transaction in binlog?
1. Source of the problem
Slow query and binlog slow transactions are commonly used methods when analyzing performance problems. Recently, I was analyzing a slow query and found that it contained a large number of commit statements that were slow, but the matching could not be completed when analyzing the binlog slow transactions. For example, there may be 1,000 commit statements during this period, but there may be only 100 slow transactions. This is too big a difference, so why does this phenomenon occur?
2. Respective judgment methods
Slow transactions usually are as follows for an explicitly submitted (insert) transaction:
GTID_LOG_EVENT and XID_EVENT are the time when the command "COMMIT" is initiated.
The first time the "INSERT" command is initiated is QUERY_EVENT.
MAP_EVENT/WRITE_ROWS_EVENT is the time when each ‘Insert’ command is initiated.
So we usually get a slow transaction time by subtracting the time of QUERY_EVENT from the time of XID_EVENT, Of course, if it is automatically submitted, it cannot be calculated like this, because Each event is the time when the statement is initiated.
Possibility of slow commit
We know that the most likely place for slow commit is flushing the binlog or waiting for semi-synchronized slave ACK. However, the time of XID EVENT in binlog does not include this part of time, which means that the commit records in binlog slow transactions and slow queries are not in the same time period.
Brief explanation
If we take the following transaction as an example, give a brief explanation
begin; insert into it values(10); commit; -- insert语句执行 -> QUERY_EVENT时间(T1) -- insert语句执行完成,判定insert语句是否为慢查询(T2) -- commit语句执行 -> GTID_LOG_EVENT和XID_EVENT时间(T3) flush fsync -----> 传输binlog (sync_binlog=1) <---- 等待ACK (rpl_semi_sync_master_wait_point=AFTER_SYNC) commit -- commit语句执行完成,判定commit语句是否为慢查询(T4)
The criterion for judging whether the insert statement is slow is T2-T1 (-lock time)
The criterion for judging whether the commit statement is slow is T4-T3
The standard for determining slow transactions is T3-T1
There is almost no overlap between the determination of slow transactions and the determination of slow commit in slow queries, so this situation is normal, as follows to prove.
3. Proof
Main database: The semi-synchronization timeout is 999999999.
Slave library: Set sync_relay_log=1, and set the breakpoint on the MYSQL_BIN_LOG::flush_and_sync function. This function is affected by sync_relay_log=1 after each event is written to the relay log from the library. Affects the decision function that must be placed on the market.
In this way, waiting at the breakpoint will significantly lengthen the commit time. It also proves that the slow semi-synchronization will affect the slow commit, as follows:
begin; select now(); -T1 insert into it values(10); select sleep(10); select now(); -T2 commit; (断点在从库生效卡主ack) -T3 select now(); -T4 结果 mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> select now(); -T1 +---------------------+ | now() | +---------------------+ | 2022-06-12 22:20:43 | +---------------------+ 1 row in set (0.00 sec) mysql> insert into it values(10); Query OK, 1 row affected (0.10 sec) mysql> select sleep(10); +-----------+ | sleep(10) | +-----------+ | 0 | +-----------+ 1 row in set (10.01 sec) mysql> select now(); -T2 AND T3 +---------------------+ | now() | +---------------------+ | 2022-06-12 22:20:54 | +---------------------+ 1 row in set (0.00 sec) mysql> commit; Query OK, 0 rows affected (21.64 sec) mysql> select now(); -T4 +---------------------+ | now() | +---------------------+ | 2022-06-12 22:21:15 | +---------------------+ 1 row in set (0.00 sec)
Let's analyze the slow query and binlog. The addition of sleep(10) prolongs the transaction commit time because the insert is too fast.
binlog slow transaction 22:20:54(T2) - 22:20:43(T1) = about 11 seconds (we added sleep(10))
# at 12221 #220612 22:20:54 server id 613306 end_log_pos 12286 CRC32 0x3e019332 GTID last_committed=40 sequence_number=41 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= '00320cc8-39f9-11ec-b5ba-000c2929706d:41'/*!*/; # at 12286 #220612 22:20:43 server id 613306 end_log_pos 12360 CRC32 0x8dcde193 Query thread_id=43 exec_time=1 error_code=0 SET TIMESTAMP=1655043643/*!*/; BEGIN /*!*/; # at 12360 #220612 22:20:43 server id 613306 end_log_pos 12409 CRC32 0x0db68582 Rows_query # insert into it values(10) # at 12409 #220612 22:20:43 server id 613306 end_log_pos 12456 CRC32 0x363a48c7 Table_map: `mysemi`.`it` mapped to number 124 # at 12456 #220612 22:20:43 server id 613306 end_log_pos 12496 CRC32 0xd44e43f3 Write_rows: table id 124 flags: STMT_END_F ### INSERT INTO `mysemi`.`it` ### SET ### @1=10 /* INT meta=0 nullable=1 is_null=0 */ # at 12496 #220612 22:20:54 server id 613306 end_log_pos 12527 CRC32 0x4d8d2c64 Xid = 547 COMMIT/*!*/;
The commit in slow query is slow 22:21:15(T4) - 22:20:54(T3) = 21 seconds
# Time: 2022-06-12T22:21:15.746223Z # User@Host: root[root] @ localhost [] Id: 43 # Schema: mysemi Last_errno: 0 Killed: 0 # Query_time: 21.641090 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0 Rows_affected: 0 # Bytes_sent: 11 SET timestamp=1655043675; commit;
It is obvious here that the slow commit of the slow query record is obviously not included in the slow transaction.
The above is the detailed content of What is the difference between slow commit in MySQL slow query and slow transaction in binlog?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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.

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.

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.

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.

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.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.
