正文同时上于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.py工具来查阅表中的页分布:

[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

可观看,第4页的<B-tree Node>, page level <0000>格式为数据页,存放着MySQL的实践数据。<Uncompressed BLOB Page>足解也MySQL存放大数额的地方,暂且叫作外部存储页。Compact格式没有将那个数量全位于数据页中,而是用一些数目在了外部存储页中。那么,是百分之百数码以表存储页中,还是有些多少。假如是如出一辙局部数据,这等同片是小为?

俺们应用hexdump -Cv row.ibd翻开转数据页<B-tree Node>, page level <0000>,也就是是第4页:

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字节的最后。注意最后的20只字节:

  • 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字节,实际只有发生4只字节约使用,所有对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|
...
...

前面38个字节为File Header(关于InnoDB数据页的详细结构要参见《MySQL技术内幕
InnoDB存储引擎》4.4),这个简单提一下:

  • cd c3 b6 8e:4字节,该页的checksum。
  • 00 00 00 04:4字节,页偏移,此页为说明空间受到的第5只页。
  • 00 00 00
    00:4字节,当前页的达标一个页。此页为<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>,所以本着下一个<Uncompressed BLOB Page>的指针也ff
ff ff ff。

通过我们得以老清晰的观数据页与BLOB页的连关系(引用淘宝数据库月报上的相同张图):
图片 1

咱俩来再次拘留一个较有意思的例证。:

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

咱们建立平等摆新表,有11只blob字段。然后为每个字段插入1000字节的数目,MySQL会提醒ERROR 1030 (HY000): Got error 139 from storage engine,什么意思为?

InnoDB是坐B+树来团数据的,假如每一行数还占有一整个Page页,那么B+树将走下坡路为单链表,所以InnoDB规定了一个Page必须带有两履行数据。也就是是单排数存储于Page上的高低约为8000字节。
而者的例证,一行数来11个1000字节的数量,Page层肯定放不产,所以当Page层留下768*11=8448字节,已经越了8000字节,所以MySQL会提示ERROR 1030 (HY000): Got error 139 from storage engine。我们死自在的定义一个字段,来囤积11000独字节,但是也无力回天将她们分成11单字段来储存,有硌意思!

那如何缓解者的问题吧?

  • 将行格式转为接下去要说的Dynamic格式。此种格式就所以20许节指向外部存储空间。
  • 将多单blob字段转为一个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

咱得望这一行数发生9只外表存储页,而我辈总计就栽了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  |.......&.......@|
...
...

因前的分析,我们现在可看出,外部存储页是匪共享的,即使一个排列的多寡差不多生一个字节,这一个字节也是垄断一个16KB空间的尺寸,这好浪费存储空间。(当然,这对准现代计算机或未是问题,呵呵)。

说了这样多,总结下Compact格式存储大数额的弱项:

  • 鉴于存在768字节的前缀在Page页,所以会有能定义一个字段,存储11000字节,但是不能够定义11个字段,每个字段存储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

第4页是数据页,第5-9页是第二前行制页。我们直接看磁盘中第4页的数据:

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地图