Mysql binlog data types
概述
对mysql binlog中,各类字段存储格式描述
数字类型
| 类型 | 大小 | 
|---|---|
| tinyint | 1 byte | 
| smallint | 2 bytes | 
| mediumint | 3 bytes | 
| int,integer | 4 bytes | 
| bigint | 8 bytes | 
| float | 4 bytes | 
| double | 8 bytes | 
| decimal(m,d),numeric(m,d) | variable | 
| bit(m) | (M+7)/8 bytes | 
tinyint, smallint, mediumint, bigint, float, double
mysql 采用小端序列存储
decimal
对于decimal(m,d)长度为变长,整数位和小数位分别存储,如 123456.3210  decimal(15,4) 8000 01e2 400c 8a
mysql将decimal中的整数位和小数位分别存储.
uint32_max = 4294967295. 对于数字999999999是可以放在一个4bytes的空间的. mysql每9位数分配一个4byte的空间, 剩余的位数按数组分配空间{0, 1, 1, 2, 2, 3, 3, 4, 4, 4};
如. decimal(15, 4) 整数位=11, 小数位=4.
那么整数位占空间为 9(4byte) + 2(1byte) = 5bytes.
小数位占空间为 4(2byte)
对应的8000 01e2 400c 8a 整数位是8000 01e2 40. 5bytes
整数位最高位为符号位. 把符号位去除. 整数位是00 0001e240 –> 00 123456
小数位是0c 8a 2byte –> 3210
时间类型
| 类型 | 大小 | 
|---|---|
| year | 1 byte | 
| date | 3 bytes | 
| time | 3 bytes + 精度 | 
| datetime | 8 bytes | 
| timestamp | 4 bytes + 精度 | 
date
小端序列,占3bytes. 5 bit 表示day, 4 bit 表示month,15 bit 表示 year
| Bits | Field | Value | 
|---|---|---|
| 10 | year | (0-9999) | 
| 4 | month | (1-12) | 
| 5 | day | (0-31) | 
即yyyyyyyy.yyyyyyym.mmmddddd
如2019-05-08表示为a8 c60f
换成大端序列
    0f        c6          a8
0000  1111 1100 0110 1010 1000
        year          month   day
|0000 1111 1100 011 | 0101 | 01000   
        2019           5       8
time
大端序列,占3bytes.
| Bits | Field | Value range | 
|---|---|---|
| 1 | sign | (Used for sign, when on disk) | 
| 1 | unused | (Reserved for wider hour range, e.g. for intervals) | 
| 10 | hour | (0-838) | 
| 6 | minute | (0-59) | 
| 6 | second | (0-59) | 
| 24 | microseconds | (0-999999) | 
| 即 xxhhhhhh.hhhhmmmm.mmssssss | 
如16:35:43表示为'81 08eb'
    81       08         eb
1000 0001 0000 1000 1110 1011
        hour     minute     second
10|00 0001 0000| 1000 11 | 10 1011
        16         35         43
额外变长字节表示精度
datetime
大端序列,占8 bytes.
| Bits | Field | Value | 
|---|---|---|
| 1 | sign | (used when on disk) | 
| 17 | year*13+month | (year 0-9999, month 0-12) | 
| 5 | day | (0-31) | 
| 5 | hour | (0-23) | 
| 6 | minute | (0-59) | 
| 6 | second | (0-59) | 
| 24 | microseconds | (0-999999) | 
Total: 64 bits = 8 bytes
如2019-05-21 14:33:22表示为99A32AE856
    99       a3        2a         e8        56
1001 1001 1010 0011 0010 1010 1110  1000 0101 0110
s         y*13+m              day    hour     minute   second
1 | 001 1001 1010 0011 00 | 10 101 |0 1110 | 1000 01 | 01 0110
                              21      14        33        22
timestamp
大端序列,占 4 bytes. 如果有精度.需要额外字节表示精度 4 bytes表示自1970-01-01 00:00:00以来的秒级数
如2019-05-13 11:51:34表示为5CD8E9C6
0x5CD8E9C6 = 1557719494