Why does MySQL rollback cause an increase in ibd files? _ MySQL

Source: Internet
Author: User
A simple test: starttransaction; insertintotb1values (3, repeat (#39; a #39;, 65000), #39; x #39;, 1); -- commit; rollback; these are the size of files after tb1.ibd is inserted, before rollback, and after Rollback: someone asked in the QQ group :... A simple test:

Start transaction;

Insert into tb1 values (3, repeat ('A', 65000), 'X', 1 );

-- Commit;

Rollback;

These are the sizes of files after tb1.ibd is inserted, before rollback, and after Rollback:

Someone asked in the QQ group: Why does rollback increase the disk space of tb1.idb?

First: 131072-98304 = 163840-131072 = 32768

Here we need to store 65000 characters 'A', so the field type should be text. while the processing of the text field is very special:

Each BLOB or TEXT value is represented internally by a separately allocated object. This is in contrast

To all other data types, for which storage is allocated once per column when the table is opened.

That is to say, for text/blob fields, they are allocated with a dedicated object for storage, and their processing is not cached in the memory, but directly written to the disk. Therefore, when the request is not submitted, you can see that tb1.idb has increased.

For the rollback of the insert operation, the previously inserted data must be deleted, while the delete operation is a logical operation, that is, setting a flag, therefore, a special object is assigned to store the object with the deletion flag set. As a result, the disk space has increased twice (this can only be explained for the moment...), each of which is 32768 bytes (15 to the power of 2 ). Once submitted or rolled back, the disk space can be recycled.

Why 32768?

The maximum storage space of text is 65656 bytes, and the minimum unit for each allocation is 32768 bytes.

That is, insert and rollback both lead to an increase in disk space allocation for the text field, and the minimum unit of allocation is 32768.

How do you replace repeat ('A', 65000) with: repeat ('A', 65535) during the test, you will find that tb1.idb increases by 65536 during insert, when rollback is executed, tb1.idb increases by 32768 again.

Therefore, rollback will cause the text field to be allocated another space, and the minimum allocation unit is 32768. It may be related to the storage structure of the text field. You need to check the source code.

Tests show that rollback operations for other non-text fields will not increase the ibd file size.

The test table creation statement is as follows:

Mysql> show create table tb1 \ G

* *************************** 1. row ***************************

Table: tb1

Create Table: create table 'tb1 '(

'Id' int (11) not null AUTO_INCREMENT,

'Webtext' text COLLATE utf8mb4_bin,

'TP 'varchar (2) COLLATE utf8mb4_bin default null,

'Se' int (11) default null,

Primary key ('id ')

) ENGINE = InnoDB AUTO_INCREMENT = 8 default charset = utf8mb4 COLLATE = utf8mb4_bin

1 row in set (0.00 sec)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.