MySQL rollback (summary sharing)
This article brings you relevant knowledge about mysql. It mainly organizes rollback-related issues and mainly introduces bookstore rollback and rollback mechanism. Let’s take a look at it together. ,I hope everyone has to help.
Recommended learning: mysql video tutorial
We often encounter operating a large table and find that the operation time is too long or affects the online Business needs to roll back large table operations. After stopping large table operations, waiting for rollback is a very long process. Although you may know some methods to shorten the time and are in awe of the integrity of the data in the production environment, you may choose not to intervene.
Transaction rollback
A transaction is an execution unit in a relational database and can be submitted or rolled back through the final stage control. Perform rollback operations in various scenarios where integrity cannot be guaranteed. Rollback in MySQL is accomplished through the Undo log, which contains information about how to undo the latest changes related to the transaction. Undo logs exist in the Undo log segment, and the Undo log segment is included in the rollback segment. The rollback segment is located in the undo table space and the global Temporary table space.
The relationship is as follows:
- undo file
mysql > show variables like '%undo%'; +--------------------------+--------------------+ | Variable_name | Value | +--------------------------+--------------------+ | innodb_max_undo_log_size | 1073741824 | | innodb_undo_directory | /opt/data8.0/mysql | | innodb_undo_log_encrypt | OFF | | innodb_undo_log_truncate | ON | | innodb_undo_tablespaces | 2 | +--------------------------+--------------------+ 5 rows in set (0.00 sec)
refers to the global Temporary A temporary tablespace (ibtmp1) that stores rollback segments for changes to user-created temporary tables.
mysql > SELECT @@innodb_temp_data_file_path; +-------------------------------+ | @@innodb_temp_data_file_path | +-------------------------------+ | ibtmp1:128M:autoextend:max:30G | +-------------------------------+
After understanding the files included in the rollback, continue reading.
Rollback mechanism:
MySQL rollback control is coordinated by the internal innodb engine and does not provide a human-controlled mechanism. The MySQL rollback parameters currently provided are as follows:
mysql> SHOW VARIABLES LIKE '%ROLL%'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_rollback_on_timeout | OFF | | innodb_rollback_segments | 128 | +----------------------------+-------+
innodb_rollback_on_timeout:
By default, InnoDB only rolls back the last statement when the transaction times out. If --InnoDB -rollback-on-timeout is specified, a transaction timeout will cause InnoDB to abort and roll back the entire transaction. It is turned off by default, once the specified time, such as rollback fails. It is conceivable that there will be inconsistencies in the data. This method is not advisable.
Innodb_rollback_segments (1~128):
Defines the number of rollback segments allocated to each undo table space, and the number of global temporary table spaces allocated for transactions that generate undo records .
The number of transactions supported by the rollback segment: depends on the number of undo slots in the rollback segment and the number of undo logs required for each transaction
The number of undo slots in the officially provided rollback segment is based on InnoDB Page size related:
From the latest MySQL8.0.27 source code implementation storage\innobase\include\trx0rseg.h:
/* Number of undo log slots in a rollback segment file copy 这里 UNIV_PAGE_SIZE正常页面的大小 即 1024*/ #define TRX_RSEG_N_SLOTS (UNIV_PAGE_SIZE / 16) /* Maximum number of transactions supported by a single rollback segment 单个回滚段支持的最大事务数1024/2=512 */ #define TRX_RSEG_MAX_N_TRXS (TRX_RSEG_N_SLOTS / 2)
By default page 1024 slots (TRX_RSEG_N_SLOTS) are divided into it, and each slot corresponds to an undo log object. Therefore, theoretically InnoDB can support 128 * 512 = 65536 ordinary transactions.
For the principle part, please refer to MySQL · Engine Features · InnoDB undo log roaming
Officially provides undbo rollback concurrent read and write scenarios:
Return from the principle of appeal In actual application scenarios:
The ability to support rollback segments is still considerable, but it is often very slow when executing large batches of rollbacks. Especially during online processing, it may take 10 minutes to roll back 100,000 rows. Or even longer.
The following uses sysbench to prepare 50 million single table data. Under no load, delete it for about 1 minute, and then use kill -9 to force stop the transaction to roll back the transaction:
Obviously the effect of restarting is better.
However, the kill -9 method can easily damage the data page, and there is a great risk. The database is also under load in daily life. It can be imagined that the cost of rolling back large transactions is very high.
Summary
Large rollback operations should be avoided as much as possible, which consumes database resources and performance, and can lead to major production accidents in a production environment. If large transaction rollback cannot be avoided, you can take the following methods:
- For batch operations, you can submit them in batches. For example, the
- undo space and global temporary table space of 1,000 to 5,000 rows can be adjusted appropriately. It is recommended to use 4 undo files and initialize 1G global ibtmp1. In a high-availability environment and ensure data consistency, you can promote the slave to a new master, provide services, and wait for large transactions to be rolled back.
- In extreme cases, you can use kill -9 to restart the operation. Because the amount of data is very large, mysql recovery will be slow. At this time, you need to wait for mysql to perform crash recovery. The waiting time will be different depending on the amount of data.
- If the data page is damaged or the rollback is skipped during the restart process, you can pass innodb_force_recovery=3 (no transaction rollback operation is performed.)
- Recommended learning:
The above is the detailed content of MySQL rollback (summary sharing). 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

AI Hentai Generator
Generate AI Hentai for free.

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



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.
