innodb record结构

11月 29, 2021 |

FIELD START OFFSETS
最高位=1表示该字段为null
剩下位表示该列在记录中的偏移量

EXTRA BYTES
n_fields 当前记录中列个数
byte_offs_flag=1,start offset中每列是一个字节还是两个字节
16位指向下一记录指针

一行的实际长度为 Field Start Offsets[n_fields *(2或者1,column所有字节之和大与128为2)] + Extra Bytes(6个字节) + 行id(6) + 事务id(6)+ rollback 指针(7) + 各个column内容

行REDUNDANT或者COMPACT格式
REDUNDANT:如果一个varchar列大于768个字节,那么btree中存放768个字节的前缀,然后将该字段完整内容存放到off-page中
COMPACT: btree中存放768个字节前缀, off-page存放768字节后面的内容

COMPACT 格式验证

-- 定义表空间,便于二进制查看

CREATE TABLESPACE `ts1` ADD DATAFILE 'ts1.ibd' Engine=InnoDB;
create table test.t(a int unsigned, b varchar(16), primary key(a))TABLESPACE ts1, row_format=compact ;
insert into test.t values(1,'javacoder'),(2,null);

二进制查看

09 #column b offset
00 ##null bit vector
00 00 10 00 00 #link consecutive records
00 00 00 01 #row id,
00 00 00 00 60 70 ce #roll pointer
00 00 01 84 01 10 #transaction id
6a 61 76 61 63 6f 64 65 72 #Assic javacoder
----------------------
01 #null bit vector
00 00 18 ff d1 #link consecutive records
00 00 00 02 #row id, primary key
00 00 00 00 60 70 ce #roll pointer
00 00 01 84 01 1d #transaction ID

如果没有定义主键,那么产生6字节的rowID

参考文档:

https://dev.mysql.com/doc/internals/en/innodb-field-contents.html
https://dev.mysql.com/doc/internals/en/innodb-extra-bytes.html
https://dev.mysql.com/doc/refman/5.7/en/innodb-row-format.html

Posted in: MySQL practise

Comments are closed.