MySQLMySQL 如何存款和储蓄长度较大的varchar与blob

本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/96

不久前,在工作中境遇了MySQL中怎么样存款和储蓄长度较长的字段类型难题,于是花了一周多的时光抽空学习了刹那间,并且记录下来。

MySQL大致的逻辑存款和储蓄结构在那篇小说中有介绍,做为基本概念:InnoDB
逻辑存款和储蓄结构

注:文中所指的大数据指的是长度较长的数码字段,包涵varchar/varbinay/text/blob。

Compact行格式

小编们先是来看一下行格式为Compact是什么样存款和储蓄大数量的:

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.1.73    |
+-----------+
1 row in set (0.01 sec)

mysql> show table status like 'row'\G;
*************************** 1. row ***************************
           Name: row
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 1
 Avg_row_length: 81920
    Data_length: 81920
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2017-01-04 21:46:02
    Update_time: NULL
     Check_time: NULL
      Collation: latin1_swedish_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)

我们树立一张测试表,插入数据:

CREATE TABLE `row` (
  `content` varchar(65532) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=latin1

mysql> insert into row(content) select repeat('a',65532);
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

我们运用py_innodb_page_info.pyMySQL,工具来查阅表中的页分布:

[root@localhost mysql]# python py_innodb_page_info.py -v com/row.ibd 
page offset 00000000, page type <File Space Header>
page offset 00000001, page type <Insert Buffer Bitmap>
page offset 00000002, page type <File Segment inode>
page offset 00000003, page type <B-tree Node>, page level <0000>
page offset 00000004, page type <Uncompressed BLOB Page>
page offset 00000005, page type <Uncompressed BLOB Page>
page offset 00000006, page type <Uncompressed BLOB Page>
page offset 00000007, page type <Uncompressed BLOB Page>
Total number of page: 8:
Insert Buffer Bitmap: 1
Uncompressed BLOB Page: 4
File Space Header: 1
B-tree Node: 1
File Segment inode: 1

能够观望,第四页的<B-tree Node>, page level <0000>格式为数据页,存放着MySQL的行数据。<Uncompressed BLOB Page>能够掌握为MySQL存放大数据的地点,权且叫作外部存款和储蓄页。Compact格式没有将大数目总体身处数据页中,而是将一部分数量放在了表面存款和储蓄页中。那么,是任何数目在外表存储页中,照旧有个别数码。若是是一有的数据,这一局地是有个别呢?

大家使用hexdump -Cv row.ibd查看一下数据页<B-tree Node>, page level <0000>,也正是第四页:

3073 0000c000  8c 25 17 57 00 00 00 03  ff ff ff ff ff ff ff ff  |.%.W....????????|
3074 0000c010  00 00 00 00 00 07 3a b8  45 bf 00 00 00 00 00 00  |......:?E?......|
3075 0000c020  00 00 00 00 00 02 00 02  03 a6 80 03 00 00 00 00  |.........?......|
3076 0000c030  00 7f 00 05 00 00 00 01  00 00 00 00 00 00 00 00  |................|
3077 0000c040  00 00 00 00 00 00 00 00  00 13 00 00 00 02 00 00  |................|
3078 0000c050  00 02 00 f2 00 00 00 02  00 00 00 02 00 32 01 00  |...?.........2..|
3079 0000c060  02 00 1c 69 6e 66 69 6d  75 6d 00 02 00 0b 00 00  |...infimum......|
3080 0000c070  73 75 70 72 65 6d 75 6d  14 c3 00 00 10 ff f1 00  |supremum.?...??.|
3081 0000c080  00 00 00 04 03 00 00 00  00 13 12 80 00 00 00 2d  |...............-|
3082 0000c090  01 10 61 61 61 61 61 61  61 61 61 61 61 61 61 61  |..aaaaaaaaaaaaaa|
3083 0000c0a0  61 61 61 61 61 61 61 61  61 61 61 61 61 61 61 61  |aaaaaaaaaaaaaaaa|
3084 0000c0b0  61 61 61 61 61 61 61 61  61 61 61 61 61 61 61 61  |aaaaaaaaaaaaaaaa|
3085 0000c0c0  61 61 61 61 61 61 61 61  61 61 61 61 61 61 61 61  |aaaaaaaaaaaaaaaa|
....
....
3128 0000c370  61 61 61 61 61 61 61 61  61 61 61 61 61 61 61 61  |aaaaaaaaaaaaaaaa|
3129 0000c380  61 61 61 61 61 61 61 61  61 61 61 61 61 61 61 61  |aaaaaaaaaaaaaaaa|
3130 0000c390  61 61 00 00 00 02 00 00  00 04 00 00 00 26 00 00  |aa...........&..|
3131 0000c3a0  00 00 00 00 fc fc 00 00  00 00 00 00 00 00 00 00  |....??..........|
3132 0000c3b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
3133 0000c3c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
3134 0000c3d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
...
...
4093 0000ffc0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
4094 0000ffd0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
4095 0000ffe0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
4096 0000fff0  00 00 00 00 00 70 00 63  01 a1 6c 2b 00 07 3a b8  |.....p.c.?l+..:?|

小编们得以看出,数据页中蕴藏了一片段数据,算下来一共是768字节,然后剩余部分存款和储蓄在表面存款和储蓄页中。那么数据页与外表存款和储蓄页、外部存储页与外表存款和储蓄页是什么样连接在同步的啊?

大家观看这一行:

3130 0000c390  61 61 00 00 00 02 00 00  00 04 00 00 00 26 00 00  |aa...........&..|
3131 0000c3a0  00 00 00 00 fc fc 00 00  00 00 00 00 00 00 00 00  |................|

这一行是前缀768字节的末尾。注意最后的十几个字节:

  • 00 00 00 02:4字节,代表外部存款和储蓄页所在的space id
  • 00 00 00 04:4字节,代表首先个外表页的Page no
  • 00 00 00 26:4字节,值为38,指向blob页的header
  • 00 00 00 00 00 00 fc
    fc:8字节,代表该列存在外部存款和储蓄页的总院长度。此处的值为64764,加上前缀768刚好是65532。(注意一点,即使表示BLOB长度的是8字节,实际只有伍个字节约使用,全部对于BLOB字段,存款和储蓄数据的最大尺寸为4GB。)

表明下第一个外表存款和储蓄页的头顶消息:

4097 00010000  cd c3 b6 8e 00 00 00 04  00 00 00 00 00 00 00 00  |?ö.............|
4098 00010010  00 00 00 00 00 06 b8 a2  00 0a 00 00 00 00 00 00  |......??........|
4099 00010020  00 00 00 00 00 02 00 00  3f ca 00 00 00 05 61 61  |........??....aa|
4100 00010030  61 61 61 61 61 61 61 61  61 61 61 61 61 61 61 61  |aaaaaaaaaaaaaaaa|
...
...

前三1多少个字节为File Header(关于InnoDB数据页的事无巨细结构请参见《MySQL技术内幕
InnoDB存款和储蓄引擎》4.4),这几个大致提一下:

  • cd c3 b6 8e:4字节,该页的checksum。
  • 00 00 00 04:4字节,页偏移,此页为表空间中的第④个页。
  • 00 00 00
    00:4字节,当前页的上1个页。此页为<Uncompressed BLOB Page>,所以并未上一页。
  • 00 00 00
    00:4字节,当前页的下三个页。此页为<Uncompressed BLOB Page>,所以并未下一页。
  • 00 00 00 00 00 06 b8 a2:8字节,该页最终被修改的日记体系地点LSN。
  • 00 0a:2字节,页类型,0x000A代表BLOB页。
  • 00 00 00 00 00 00 00 00:8字节,略过。
  • 00 00 00 02:页属于哪个表空间,此处指表空间的ID为2。

之后是4字节的00 00 3f ca,那里的值为16330,代表此BLOB页的实用数据的字节数。00 00 00 05表示下三个BLOB页的page
number。

我们看最终叁个<Uncompressed BLOB Page>,第8个页:

7169 0001c000  fa 78 9b 27 00 00 00 07  00 00 00 00 00 00 00 00  |?x.'............|
7170 0001c010  00 00 00 00 00 07 3a b8  00 0a 00 00 00 00 00 00  |......:?........|
7171 0001c020  00 00 00 00 00 02 00 00  3d 9e ff ff ff ff 61 61  |........=.????aa|
7172 0001c030  61 61 61 61 61 61 61 61  61 61 61 61 61 61 61 61  |aaaaaaaaaaaaaaaa|
7173 0001c040  61 61 61 61 61 61 61 61  61 61 61 61 61 61 61 61  |aaaaaaaaaaaaaaaa|
...
...

最终一页的卓有功能数据大小为0x00003d9e=15774,768+16330*3+15774 =
65532字节,符合早先插入数据的轻重。
由于那是终极二个<Uncompressed BLOB Page>,所以针对下3个<Uncompressed BLOB Page>的指针为ff
ff ff ff。

因此大家得以很分明的看看数据页与BLOB页的连续关系(引用天猫商城数据库月报上的一张图):
MySQL 1

咱俩来再看3个相比有趣的例子。:

CREATE TABLE `testblob` (
  `blob1` blob NOT NULL,
  `blob2` blob NOT NULL,
  `blob3` blob NOT NULL,
  `blob4` blob NOT NULL,
  `blob5` blob NOT NULL,
  `blob6` blob NOT NULL,
  `blob7` blob NOT NULL,
  `blob8` blob NOT NULL,
  `blob9` blob NOT NULL,
  `blob10` blob NOT NULL,
  `blob11` blob NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

mysql> insert into testblob select repeat('a',1000),repeat('b',1000),repeat('c',1000),repeat('d',1000),repeat('e',1000),repeat('f',1000),repeat('g',1000),repeat('h',1000),repeat('i',1000),repeat('j',1000),repeat('k',1000);
ERROR 1030 (HY000): Got error 139 from storage engine

笔者们建立一张新表,有拾叁个blob字段。然后向各类字段插入一千字节的数额,MySQL会唤醒ERROR 1030 (HY000): Got error 139 from storage engine,什么意思吧?

InnoDB是以B+树来协会数量的,要是每一行数据都占有一整个Page页,那么B+树将落后为单链表,所以InnoDB规定了3个Page必须含有两行数据。也正是单排数据存款和储蓄在Page上的轻重缓急大致为捌仟字节。
而地点的例子,一行数据有11个一千字节的数额,Page层肯定放不下,所以在Page层留下768*11=8448字节,已经超(英文名:jīng chāo)越了八千字节,所以MySQL会提醒ERROR 1030 (HY000): Got error 139 from storage engine。大家很轻松的定义3个字段,来储存1一千个字节,不过却无计可施将她们分成十一个字段来存款和储蓄,有点意思!

那么哪些化解地点的难点啊?

  • 将行格式转为接下去要说的Dynamic格式。此种格式只用20字节指向外部存储空间。
  • 将五个blob字段转为1个blob字段。四个字段能够用数组存款和储蓄,然后json_encode打包进blob。

大家向表中插入一条有效记录:

mysql>  insert into testblob(blob1,blob2,blob3,blob4,blob5,blob6,blob7,blob8,blob9) select repeat('a',8000),repeat('b',8000),repeat('c',8000),repeat('d',8000),repeat('e',8000),repeat('f',8000),repeat('g',8000),repeat('h',8000),repeat('i',8000);
Query OK, 1 row affected (0.12 sec)
Records: 1  Duplicates: 0  Warnings: 0

[root@localhost mysql]# python py_innodb_page_info.py -v com/testblob.ibd
page offset 00000000, page type <File Space Header>
page offset 00000001, page type <Insert Buffer Bitmap>
page offset 00000002, page type <File Segment inode>
page offset 00000003, page type <B-tree Node>, page level <0000>
page offset 00000004, page type <Uncompressed BLOB Page>
page offset 00000005, page type <Uncompressed BLOB Page>
page offset 00000006, page type <Uncompressed BLOB Page>
page offset 00000007, page type <Uncompressed BLOB Page>
page offset 00000008, page type <Uncompressed BLOB Page>
page offset 00000009, page type <Uncompressed BLOB Page>
page offset 0000000a, page type <Uncompressed BLOB Page>
page offset 0000000b, page type <Uncompressed BLOB Page>
page offset 0000000c, page type <Uncompressed BLOB Page>
Total number of page: 13:
Insert Buffer Bitmap: 1
Uncompressed BLOB Page: 9
File Space Header: 1
B-tree Node: 1
File Segment inode: 1

大家得以看出这一行数据有7个外表存款和储蓄页,而作者辈一共就插入了9列数据,是否当每一列的多寡在page页放不下,都独立申请三个外部存款和储蓄页,而互相在此之前不共享外部存储页。大家看一下page页的组织就知道了:

 3130 0000c390  61 61 61 61 61 61 61 61  61 61 61 61 61 61 61 61  |aaaaaaaaaaaaaaaa|
 3131 0000c3a0  61 61 61 61 00 00 00 05  00 00 00 04 00 00 00 26  |aaaa...........&|
...
...
 3180 0000c6b0  62 62 62 62 62 62 62 62  00 00 00 05 00 00 00 05  |bbbbbbbb........|
 3181 0000c6c0  00 00 00 26 00 00 00 00  00 00 1c 40 63 63 63 63  |...&.......@cccc|
...
...
 3229 0000c9c0  63 63 63 63 63 63 63 63  63 63 63 63 00 00 00 05  |cccccccccccc....|
 3230 0000c9d0  00 00 00 06 00 00 00 26  00 00 00 00 00 00 1c 40  |.......&.......@|
...
...

依照前边的分析,大家以后得以观望,外部存储页是不共享的,固然叁个列的数目多出3个字节,那一个字节也是总揽叁个16KB空间的轻重,那很浪费存款和储蓄空间。(当然,这对现代处理器大概寻常,呵呵)。

说了那样多,计算下Compact格式存款和储蓄大数指标毛病:

  • 由于存在768字节的前缀在Page页,所以会设有能定义三个字段,存款和储蓄1一千字节,可是不可能定义12个字段,每一种字段存款和储蓄1000字节的”bug”。
  • 表面存款和储蓄页不共享,固然多余多个字节也是独享16KB的页面。

Dynamic行格式

随后大家第二看一下行格式为Dynamic是怎么存款和储蓄大数据的:

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.14    |
+-----------+
1 row in set (0.00 sec)

mysql> show table status like 'row'\G;
*************************** 1. row ***************************
           Name: row
         Engine: InnoDB
        Version: 10
     Row_format: Dynamic
           Rows: 0
 Avg_row_length: 0
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2017-01-03 22:45:16
    Update_time: NULL
     Check_time: NULL
      Collation: latin1_swedish_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)

创办和compact格式一样的表:

CREATE TABLE `row` (
  `content` varchar(65532) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=latin1

insert into row(content) select repeat('a',65532);
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

看下页分布:

[root@localhost mysql]# python py_innodb_page_info.py -v row.ibd 
page offset 00000000, page type <File Space Header>
page offset 00000001, page type <Insert Buffer Bitmap>
page offset 00000002, page type <File Segment inode>
page offset 00000003, page type <B-tree Node>, page level <0000>
page offset 00000004, page type <Uncompressed BLOB Page>
page offset 00000005, page type <Uncompressed BLOB Page>
page offset 00000006, page type <Uncompressed BLOB Page>
page offset 00000007, page type <Uncompressed BLOB Page>
page offset 00000008, page type <Uncompressed BLOB Page>
Total number of page: 9:
Insert Buffer Bitmap: 1
Uncompressed BLOB Page: 5
File Space Header: 1
B-tree Node: 1
File Segment inode: 1

第五页是数据页,第6-9页是二进制页。大家直接看磁盘中第陆页的数目:

3073 0000c000  dc 2d b0 f5 00 00 00 03  ff ff ff ff ff ff ff ff  |.-..............|
3074 0000c010  00 00 00 00 00 a3 4b 59  45 bf 00 00 00 00 00 00  |......KYE.......|
3075 0000c020  00 00 00 00 00 36 00 02  00 a6 80 03 00 00 00 00  |.....6..........|
3076 0000c030  00 7f 00 05 00 00 00 01  00 00 00 00 00 00 00 00  |................|
3077 0000c040  00 00 00 00 00 00 00 00  00 64 00 00 00 36 00 00  |.........d...6..|
3078 0000c050  00 02 00 f2 00 00 00 36  00 00 00 02 00 32 01 00  |.......6.....2..|
3079 0000c060  02 00 1c 69 6e 66 69 6d  75 6d 00 02 00 0b 00 00  |...infimum......|
3080 0000c070  73 75 70 72 65 6d 75 6d  14 c0 00 00 10 ff f1 00  |supremum........|
3081 0000c080  00 00 00 02 00 00 00 00  00 07 07 a7 00 00 01 1b  |................|
3082 0000c090  01 10 00 00 00 36 00 00  00 04 00 00 00 26 00 00  |.....6.......&..|
3083 0000c0a0  00 00 00 00 ff fc 00 00  00 00 00 00 00 00 00 00  |................|
3084 0000c0b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
3085 0000c0c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
3086 0000c0d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
3087 0000c0e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
...
...
...

和Compact格式有着鲜明的差异,当大数据在Page页存放不下时,Dynamic行格式不会留768字节在Page页,并且将全部大数目都置身外部存储页。具体的数据页和表面存款和储蓄页的一而再关系同Compact格式一样。

笔者们再看看Dynamic格式的外表存款和储蓄页是还是不是每三个列独享外部存款和储蓄空间,依旧同Compact格式实验进度同样:

CREATE TABLE `testblob` (
  `blob1` blob NOT NULL,
  `blob2` blob NOT NULL,
  `blob3` blob NOT NULL,
  `blob4` blob NOT NULL,
  `blob5` blob NOT NULL,
  `blob6` blob NOT NULL,
  `blob7` blob NOT NULL,
  `blob8` blob NOT NULL,
  `blob9` blob NOT NULL,
  `blob10` blob NOT NULL,
  `blob11` blob NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

mysql>   insert into testblob(blob1,blob2,blob3,blob4,blob5,blob6,blob7,blob8,blob9,blob10,blob11) select repeat('a',8000),repeat('b',8000),repeat('c',8000),repeat('d',8000),repeat('e',8000),repeat('f',8000),repeat('g',8000),repeat('h',8000),repeat('i',8000),repeat('j',8000),repeat('k',8000);
Query OK, 1 row affected (0.10 sec)
Records: 1  Duplicates: 0  Warnings: 0

看一下外部存款和储蓄页数据:

 4599 00011f60  61 61 61 61 61 61 61 61  61 61 61 61 61 61 00 00  |aaaaaaaaaaaaaa..|
 4600 00011f70  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

好的,能够不用向下看别的列的了,Dynamic的外部存储页也不是共享的。

唯独MySQL为何要如此设计啊?可能是为了贯彻容易吗,沿着链表通过立见成效数据大小就能读取blob的全套多少。假设多少个字段的blob混在联名,或许设计更复杂,要更新每种字段的偏移量之类的,更新的话页数据管理也正如劳累。作者的民用猜测,呵呵。

小结下Dynamic格式存款和储蓄大数目标特点:

  • 当数码页放不下时,MySQL会将大数量总体身处外部存款和储蓄页,数据页只留指向外部存款和储蓄页的指针。
  • 外表存款和储蓄页不共享,即便多余一个字节也是独享16KB的页面。

鉴于有较多的尝试进程,所以显得相比较乱,提出看到那篇作品人温馨执行一回,究竟本人动手会思考越来越多的题材与细节,掌握的也正如深刻,哈哈哈。

参考资料:http://dev.mysql.com/doc/refman/5.7/en/column-count-limit.html
http://mysqlserverteam.com/externally-stored-fields-in-innodb/
https://www.percona.com/blog/2010/02/09/blob-storage-in-innodb/
http://mysql.taobao.org/monthly/2016/02/01/

网站地图xml地图