wing324 2014-12-22 06:14 采纳率: 0%
浏览 642

mysql 5.6innodb compac的vachar长度标记位1还是2byte的问题:

For each non-NULL variable-length field, the record header contains the length of the column in one or two bytes. Two bytes will only be needed if part of the column is stored externally in overflow pages or the maximum length exceeds 255 bytes and the actual length exceeds 127 bytes. For an externally stored column, the 2-byte length indicates the length of the internally stored part plus the 20-byte pointer to the externally stored part. The internal part is 768 bytes, so the length is 768+20. The 20-byte pointer stores the true length of the column.

5.6文档上有如上的一句话,文档上说当最大长度超过255字节和实际存储超过127字节的时候,使用2Byte标识,是什么意思呢?那如果最大长度超过255,实际存储小于127byte呢?

  • 写回答

1条回答 默认 最新

  • 昔久 2020-04-02 10:29
    关注

    楼主好,主要问题是这句话比较关键:

    Two bytes will only be needed if part of the column is stored externally in overflow pages or the maximum length exceeds 255 bytes and the actual length exceeds 127 bytes.

    也就是如果需要2个字节存储的话,需要满足二选一

    • 1、part of the column is stored externally in overflow pages
    • 2、 the maximum length exceeds 255 bytes and the actual length exceeds 127 bytes

    你是对条件2有疑问,条件二是说“当前列的最大长度需超过255byte,并且当前记录的实际长度需超过127byte,二者缺一不可”; 这个描述是没有问题的,255正好是无符号byte的最大长度。

    举例说明:
    1. 假如某列a的最大长度设定为200,实际存储长度为190,虽然长度超过了127,但是最大长度在255内,也就是一个字节完全可以存储,此时只需1个字节
    2. 假如某列a的最大长度设定为300,实际存储长度为190,此种情况下已经不能保证1个字节能存储下该列的实际长度,所以需要2个字节
    3. 假如某列a的最大长度设定为300,实际存储长度为80,(就是楼主说的最大长度超过255,实际存储小于127)此种情况虽然最大超过了一个字节,不过实际存储小于127,所以只需1个字节

    还有同学会问,为什么实际存储长度的临界值要设置为127,使用255不好么?那是因为最高位是用来判断是否需要2个byte存储的,mysql实际在读取记录的时候,只会读取1个byte,通过最高位是否为1,来判断使用1个字节or2个字节存储,如果最高位为1,那还会读取第二个字节

    thats all...

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题