[MySQL] storage engine (four): InnoDB disk structure (table space)

The disk structure is mainly a variety of table spaces, called Table space. The table space can be regarded as the highest level of the logical structure of the InnoDB storage engine, and all data is stored in the table space . InnoDB's table space is divided into 5 categories.

1.system tablespace (system tablespace)

By default, the InnoDB storage engine has a shared tablespace (corresponding to the file /var/lib/mysql/ibdata1), also called the system tablespace.

Insert picture description here
The InnoDB system tablespace contains InnoDB data dictionary and double write buffer, ChangeBuffer and UndoLogs). If file-per-table is not specified, it also contains table and index data created by users.

  • undo will be introduced later because there are independent tablespaces.
  • Data dictionary: It is composed of internal system tables and stores metadata (definition information) of tables and indexes.
  • Double write buffering (a major feature of InnoDB): InnoDB pages and operating system page sizes are inconsistent, InnoDB page size is generally 16K, operating system page size is 4K, when InnoDB pages are written to disk, a page needs to be divided into 4 Time to write.

Insert picture description here

If the storage engine crashes while writing the page data to the disk, there may be a situation where the page is only partially written, for example, when only 4K is written, it crashes. This situation is called partial page write failure (partial page write) , May cause data loss.

show variables like 'innodb_doublewrite';

Insert picture description here

Don’t we have a redo log? But there is a problem. If the page itself has been damaged, it is meaningless to use it for crash recovery. So before applying the redo log, a copy of the page is required. If a write failure occurs, use a copy of the page to restore the page, and then apply the redo log. The copy of this page is double write , InnoDB's double write technology. Through it, the reliability of the data page is realized.

Like redo log, double write consists of two parts, one part is double write in memory, and the other part is double write on disk. Because double write is written sequentially, it will not bring a lot of overhead. By default, all tables share a system table space, this file will grow larger and larger, and its space will not shrink.

2.file-per-table tablespaces (exclusive table space)

We can make each table occupy a table space exclusively. This switch is set by innodb_file_per_table and is turned on by default.

SHOW VARIABLES LIKE 'innodb_file_per_table';

Insert picture description here

After opening, each table will open up a table space, this file is the ibd file in the data directory (for example
/var/lib/mysql/forum/user_innodb.ibd), which stores the index and data of the table.
Insert picture description here

But other types of data, such as undo information, insert buffer index pages, system transaction information, double write buffer, etc. are still stored in the original shared table space.

3.general tablespaces (general tablespace)

The general table space is also a shared table space, similar to ibdata1. A general table space can be created to store tables in different databases, and the data path and files can be customized. grammar:

create table space ts2673 add datafile '/var/lib/mysql/ts2673.ibd' file_block_size=16K engine=innodb;

You can specify the table space when you create the table, and use ALTER to modify the table space to transfer the table space.

create table t2673(idinteger) tablespace ts2673;

Data in different table spaces can be moved. To delete a table space, you need to delete all the tables inside:

drop table t2673; 
drop tablespace ts2673;

4. Temporary tablespaces (temporary table space)

Store the data of temporary tables, including temporary tables created by users and internal temporary tables on disk. Corresponding to the ibtmp1 file in the data directory. When the data server shuts down normally, the table space is deleted and regenerated next time.

5.undo log tablespace

The undo log (undo log or rollback log) records the data state before the transaction (not including select). It is used to ensure rollback when necessary. If another transaction needs to view the original data in a consistent read operation, the unmodified data is retrieved from the undo log record, which means that the MVCC mechanism also relies on the undo log to achieve. When the undo is executed, the data is only logically restored to the state before the transaction, rather than realized from the operation on the physical page, which belongs to the logical format of the log.

Redo Log and undo Log are closely related to transactions and are collectively referred to as transaction logs . But the difference with redo log is:

  • Redo log stores physical logs, undo log stores logical logs
  • redo log redo logs, and roll-front operation; undo log is a rollback log, providing a rollback operation.

undo is a version of the modified data saved before the start of the transaction. When the undo log is generated, it will also be accompanied by the generation of a redo log similar to the protection transaction persistence mechanism.

  • Redo recording a data block to be modified after the value, the data can be used to restore the data file is not written in the transaction have been successfully updated. - Guarantee transaction durability
  • Undo a recording data is modified before the values can be used to rollback the transaction fails; - guarantee atomicity


For example, the database is down at a certain moment, and there are two transactions. One transaction has been committed and the other transaction is being processed. When the database is restarted, it is necessary to roll forward and roll back according to the log, write the changes of the committed transaction to the data file, and restore the changes of the uncommitted transaction to the state before the transaction started.

  • When the data is crash-recovery, use redo log to recover all transactions that have been committed inside the storage engine through redo log
  • All transactions that have been prepared but not committed will be rolled back using undo log

The undo Log data is in the system tablespace ibdata1 file by default, because the shared tablespace will not automatically shrink, you can also create a separate undo tablespace.

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43935927/article/details/113983374