Analysis of slow insert cases caused by large transactions in MySQL

Analysis of slow insert cases caused by large transactions in MySQL

【question】

The INSERT statement is one of the most common SQL statements. Recently, a MySQL server has been reporting concurrent thread alerts from time to time. From the log information, there are a large number of slow insert queries that take dozens of seconds to execute, wait for flushing logs, and are in the query end state.

[Preliminary analysis]

From the perspective of waiting for resources, most of the time is spent in the innodb_log_file stage. It is suspected that it may be caused by disk problems. After investigation, no hardware problems were found in the server itself.

Later, when the thread increases, the automatic collection of pstack is enabled to locate the position where the MySQL thread is waiting.

【Analysis process】

After deploying the automatic crawling of pstack, there were 6 thread concurrency >= 50 alarms (each time an alarm was issued, a large number of slow queries were generated), and 3 of them were caught on the spot.

When the number of concurrent threads increases, more than 50 threads are stuck in Stage_manager::enroll_for function, in the group commit stage

The SQL statement corresponding to thread 0x519c5940 is as follows and has been executed for 18 seconds

The purpose of Stage_manager::enroll_for function is to implement the queuing of multiple threads in the flush_stage stage. Simply put, for a grouped transaction, it is submitted by the leader thread, and other threads are in a queue waiting state, waiting for the leader thread to submit the transaction of the thread.

If the first thread executes slowly, the subsequent threads are in a waiting state and the entire group of transactions cannot be committed.

The process can also be understood as follows:

Session A COMMIT-->Get the lock-->Write binlog-->Commit completed

Session B COMMIT-->Wait for lock--------------------------->Get lock-->Write binlog-->Commit completed

Why is the first thread executing so slowly? We analyzed the log files during the alarm period and found that there were two large transactions of 15M and 20M in the log files.

Check the log details. There is a large transaction deletion statement called "delete from", which contains about 230,000 records. Deleting 230,000 records in ROW mode will generate a log file of about 20 MB. The disk flushing time is long, which blocks the submission of other transactions in the same group.

The start time of the transaction coincides with the alarm time

The backlogged grouped transactions are flushed to disk in a concentrated manner. This is reflected in the disk index. The disk_write_kbytes index shows a significant increase during the problem period.

【Optimization plan】

1. It is recommended that developers avoid using the delete from statement to delete a large transaction of the entire table.

[Other workarounds]

2. Binlog records in ROW mode will generate a large number of logs. Changing to MIXED mode can theoretically solve the problem.

3. Replace the disk with one with better performance

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Reasons why MySQL 8.0 statistics are inaccurate
  • How to quickly import data into MySQL
  • 5 MySQL GUI tools recommended to help you with database management
  • Centos7 mysql database installation and configuration tutorial
  • How to crawl 51cto data with Python and store it in MySQL
  • How to shrink the log file in MYSQL SERVER
  • Why MySQL should avoid large transactions and how to solve them

<<:  Solution to nginx not jumping to the upstream address

>>:  A brief discussion of 12 classic problems in Angular

Recommend

How to reasonably use the redundant fields of the database

privot is the intermediate table of many-to-many ...

About the problem of offline installation of Docker package on CentOS 8.4

The virtual machine used is CentOS 8.4, which sim...

How to execute Linux shell commands in Docker

To execute a shell command in Docker, you need to...

JavaScript Composition and Inheritance Explained

Table of contents 1. Introduction 2. Prototype ch...

CentOS7 installation zabbix 4.0 tutorial (illustration and text)

Disable SeLinux setenforce 0 Permanently closed: ...

MySQL 5.5 installation and configuration graphic tutorial

Organize the MySQL 5.5 installation and configura...

Causes and solutions for MySQL master-slave synchronization delay

For historical reasons, MySQL replication is base...

JavaScript to implement dynamic digital clock

This article shares the specific code for impleme...

How InnoDB implements serialization isolation level

Serialization implementation InnoDB implements se...