"How does Mysql run" reading notes five

Storage of records in the page

InnoDBThe storage space of a data page is roughly divided into 7 parts, as shown in the following table.

name Chinese name Occupied space size Quick description
File Header File header 38 bytes Some general information on the page
Page Header Page header 56 bytes Some information specific to the data page
The highest Infimun + 26 bytes Mark whether the record is deleted Two virtual records
User Records User record uncertain User stored record content
Free Space Free space uncertain Unused space on the page
Page Directory Page directory uncertain The relative position of some records on the page
File Trailer End of file 8 bytes Check whether the page is complete

User RecordsIt is used to store user records, but when the page is generated at the beginning, there is no User Recordspart. Whenever a piece of data is inserted, Free Spacethe size of a record is applied from the part, and the space is divided into User Recordsparts. When the Free Spacepart of the space is all After being User Recordspartially replaced, it means that this page is also used up. If you insert new data, you need to apply for a new page.
In order to better manage User Recordspart of the record, it is necessary to study the record header information of the line record first.
This is a line format diagram.

Variable length field length list List of NULL values Record header information Column 1 value Column 2 value ··· Column 3 value

Let's take out the record header information in the row format. The following is the general meaning of each attribute of the record header information.

name Size (bits) description
Reserved 1 1 did not use
Reserved 2 1 did not use
deleted_flag 1 Mark whether the record is deleted
min_rec_flag 1 This mark will be added to the smallest directory entry record in each non-leaf node in the B+ tree
n_owned 4 The records in a page will be divided into several groups. In each group, one record is "Leader Brother", and the rest of the records are "Little Brother". The n_ownedvalue of the "Leader Brother" record represents the number of all records in the group. , The n_ownedvalue recorded by "Little Brother" is 0
heap_no 13 Indicates the relative position of the current record in the page heap
record_type 3 Indicates the type of the current record, 0 means normal record, 1 means directory entry record of B+ tree non-leaf node, 2 means Infimunrecord, 3 means Supremumrecord
next_record 16 Indicates the relative position of the next record

deleted_flag: Used to mark whether the current record has been deleted, 0 means it has not been deleted, 1 means it has been deleted. These deleted records are not removed from the disk. All deleted records form a garbage linked list. The space recorded in this linked list is called reusable space. After new records are inserted into the table, they may be overwritten. The space occupied by these deleted records.
min_rec_flag: This mark is added to the smallest directory entry record in each non-leaf node of the B+ tree.
n_owned: The last record of each group in the page (that is, the largest record in the group). The header information n_ownedattribute of this record indicates that there are several records in the group, and the n_ownedvalue of the remaining records in the group is 0.
heap_no: The data we insert into the table are all stored in User Records, these records are arranged one by one closely, as shown in the following figure:

We call this structure a heap, and the position of a record in the heap is called heap_no, in The records at the front of the page are heap_norelatively small, and the records at the back of the page are heap_norelatively large. Every time the storage space of a record is reapplied, the value of this record is 1 greater than the heap_novalue of the record in front of it .
There will be two pseudo records in each page. These two records are not added by the user. One represents the smallest record ( Infimumrecord) on the page and the other represents the largest record ( Supremumrecord) on the page .

Usually to compare the size of a record is to compare the size of the primary key.
InfimumRecords and Supremumrecords do not have a primary key value, but it is artificially stipulated that the Infimumrecord is the smallest Supremumrecord in a page, and the record is the largest record in a page.
InfimumRecorded and Supremumrecorded heap_noare 0 and 1 , respectively.

record_type: This attribute represents the type of the current record, there are 4 types in total:

  • 0 means normal record;
  • 1 represents the directory entry record of the B+ leaf node;
  • 2 means Infimumrecord;
  • 3 means Supremumrecord;

next_record: Indicates the distance from the real data of the current record to the real data of the next record. If the value is positive, it means that the next record of the current record is in front of the current record. If it is negative, it means that the next record of the current record is in the current record. Behind the record.

The next record does not refer to the next record in the insertion order, but the next record arranged in descending order of the primary key value.

The records form a linked list in the descending order of the primary key. SupremumThe next_recordvalue of the record is 0, which means that there is no next data, which means next_recordthat it is the last node in the singly linked list.
No matter how to add, delete, check, and modify, InnoDBa singly linked list of records will always be maintained. The nodes of this linked list are linked in descending order of primary key value.

Page directory

Now we know that the records in the page are concatenated into a singly linked list in the order of primary key value from small to large. When we execute the following query, how do we query it?

select * from page_demo while c1=3;

Instead of starting from the Infimumrecord and looking backward along the singly linked list, it uses a book-like catalog method. The process is as follows:

  1. Divide all normal records (including Infimumrecords and Supremumrecords, but not records that have been removed) into several groups.
  2. The last record of each group (that is, the largest record in the group). The header information n_ownedattribute of this record indicates how many records are in the group.
  3. The address offset of the last record in each group is extracted separately from the page and stored in order to the end of the page. These address offsets in the page directory are called slots, and each slot occupies two words Section, page directory is composed of multiple slots.

And the number of records in each group is specified: there Infimumcan only be one record Supremumfor the group where the record is located, and the number of records in the group where the record is located can only be between 1-8, and the remaining grouping number range Between 4-8, the steps are as follows:

  1. In the initial situation, a data page has only two records, Infimumrecord and Supremumrecord, which belong to two groups, and there are also two slots in the page directory.
  2. After each insertion of a record, the corresponding primary key value will be found from the page directory for a slot with a larger and smallest difference than the primary key value of the record to be inserted, and then the n_owndvalue of the record corresponding to the slot will be increased by 1.
  3. When the number of records in a group is equal to 8, inserting another record will split the records in the group into two groups, one of which contains 4 records and the other 5 records. This splitting process will be in the page directory A new slot is added to record the offset of the largest record in this new group.

Therefore, when searching for a record with a specified primary key value in a data page, it is divided into the following two steps.

  1. Determine the slot corresponding to the group where the record is located by dichotomy, and then find the record with the smallest primary key value in the group where the slot is located.
  2. next_recordTraverse the records in the group where the slot is located through the record attributes.

Guess you like

Origin blog.csdn.net/weixin_43213064/article/details/111599807