How MySQL works--Chapter 5

Posted by charlesg on Tue, 01 Feb 2022 13:19:19 +0100

5.1

There are many types of pages that record data, such as pages for INODE information, pages for undo logs, and so on. This chapter explores the pages where records are stored (INDEX).

5.2

File Header		General information for pages
 Page Header		Some information unique to data pages
 Minimum Records on Page+Maximum Record	Two dummy records
 User Records		Record contents stored by the user
 Free space		Unused space on page
 Page Directory			Relative position of some records in a page
 End of file		Check page integrity

5.3

The part about the data actually stored by the user is User Records. This section is empty until the user inserts the data. When users insert data, they request a record space from FreeSpace. When the space in the FreeSpace section is completely replaced by UserRecord, this means that the page is exhausted. If there are any new records, you need to apply for a new page.

First create a table and insert some records

CREATE TABLE page_demo(
	c1 INT,
	c2 INT,
	C3 VARCHAR(10000),
	PRIMARY KEY (c1)
) CHARSET=ascii ROW_FORMAT=COMPACT;
INSERT INTO page_demo VALUES(1, 100, 'aaaa'), (2, 200, 'bbbb'), (3, 300, 'cccc'), (4, 400, 'dddd');

Tag bit 1 deleted_flag indicates whether deleted
Tag Bit 2 min_rec_flag indicates whether it is the smallest directory entry record for an internal leaf node
Tag Bit 3 n_owned indicates whether it is a big brother, and if it is, how many records are smaller than it
Tag Bit 4 heap_no represents the relative position recorded in the heap. Heap_ Records of no 0 and 1 are fixed, being the minimum record and the maximum record, respectively.
Tag bit 5 record_ The type represents the type of the current record. There are four types, namely, normal record, directory entry record of B+ tree inner leaf node, Infimum record, Supremum record.
Tag Bit 6 next_ A record represents the distance from the actual data of the current record to the actual data of the next record. If it is positive, look down, and if it is negative, look up.

Maximum and minimum records are hard. Comparing the sizes between records depends on the primary key.
Both records are structurally fixed, with 5-byte record headers and 8-byte fixed words. The word is your own English.

heap_no Values do not change after they are allocated.

next_record The previous and next entries in the are not in insertion order, but in order after the primary key. This forms a list of chains.
As to why it is the location of the real data, it is more convenient. Head to the left and data to the right.
and null Value tables and variable-length field length tables are stored in reverse order, which allows data to be closer to the header and increases cache hit rates.
For deleted records, you can use their next_record Fields form a list of garbage chains for future reuse.

5.4

How do I find a record on the page?
The silly way to do this is to start with the first record in the user record and start looking one by one. But this is too inefficient.
A more normal idea is two. What exactly does it look like in innodb?

Divide all records into groups.
The last record in each group is equivalent to "Big Brother" and the remaining records in each group are equivalent to "Little Brother".
"Big Brother's n_owned Attributes indicate how many records are in this group.
Extract the address offset of the last recorded data area of each group from the page and save it to Page Directory Zone.
These address offsets on a page are called slot,each slot Two bytes.

In this case, divide the groups into two and finally scan the different groups.
But what is the basis for group division?

Hard regulations: Infimum Records can only be in one group. Supremum Recorded groups can only have 1-8 Records.
Other groupings can have 4-8 Records.

Initially, there are only two groups.
Each subsequent insert will find a group that is just a little larger than the primary key of the record to be inserted and insert the record into that group.
If the insertion causes the group to become 8, then inserting another one will split the group into two groups, the first group with four records and the second group with five records.

5.5

The header is complex.

Here's where the insertion direction needs to be described

If a newly inserted record has a higher primary key value than the previous record, the insertion direction for that record is right, otherwise it is left.
InnoDb Writes down the number of records inserted in one direction, using PAGE_N_DIRECTION State representation.

5.6

The previous section focused on information about the status of the data page. This section describes the header, which is common information.

Page number is the only id that represents a page.

Where FIL_ PAGE_ There are many alternative types of TYPE.

Since a table may hold a large amount of data and require many pages, allocating space is not necessarily a contiguous space allocated. Therefore, the numbers of the previous and next pages need to be recorded to form a chain table.

5.7

This section records the end of the file.
The end of the file is mainly for checking.
When we brush the data disc, it is possible that half of the brush disc machine is down.
To solve this problem, we added the end of the file.

The end of the file consists of eight bytes, the first four bytes being the checksum and the last four bytes being the LSN. 
The first 4 bytes should match the FileHeader The checksum in is the same. Therefore, if the two checks are different after the modification, an error has occurred.
The last four bytes is the last modified log sequence number value, as it should be in the header. It is also the function of verification.

Topics: Database MySQL