MySQL CHAR和VARCHAR存储、读取时的差别
作者:Lenix 时间:2024-01-22 20:54:58
导读
你真的知道CHAR和VARCHAR类型在存储和读取时的区别吗?
还是先抛几条结论吧:
1、存储的时候,CHAR总是会补足空格后再存储,不管用户插入数据时尾部有没有包含空格。
2、存储的时候,VARCHAR不会先补足空格后再存储,但如果是用户在插入时特地加了空格那就会如实存储,而不会给删除。
3、读取数据时,CHAR总是会删除尾部空格(哪怕是写入时包含空格)。
4、读取数据时,VARCHAR总是如实取出之前存入的值(如果存储时尾部包含空格,就会继续保留着,不会像CHAR那样删除尾部空格)。
下面是测试验证过程。
1、测试CHAR类型
表结构:
CREATE TABLE `tchar` (
`id` int(10) unsigned NOT NULL DEFAULT '0',
`c1` char(20) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
插入几条记录:
insert into tchar values (1, concat('a', repeat(' ',19)));
insert into tchar values (2, concat(' ', repeat('a',19)));
insert into tchar values (3, 'a');
insert into tchar values (4, ' ');
insert into tchar values (5, '');
查看存储结构:
(1) INFIMUM record offset:99 heapno:0 ...
(2) SUPREMUM record offset:112 heapno:1 ...
(3) normal record offset:126 heapno:2 ... <- id=1
(4) normal record offset:169 heapno:3 ... <- id=2
(5) normal record offset:212 heapno:4 ... <- id=3
(6) normal record offset:255 heapno:5 ... <- id=4
(7) normal record offset:298 heapno:6 ... <- id=5
看到这坨东西有点懵是不是,还记得我给你们安利过的一个工具不,看这里:innblock | InnoDB page观察利器。
可以看到,无论我们存储多长的字符串进去,每条记录实际都是占用43(169-126=43)字节。由此结论1成立。
简单说下,43字节的由来:
DB_TRX_ID, 6字节。
DB_ROLL_PTR, 7字节。
id, int, 4字节。
c1, char(20), 20字节;因为是CHAR类型,还需要额外1字节。
每条记录总是需要额外5字节头信息(row header)。
这样总的加起来就是43字节了。
再看下读取tchar表的结果:
select id,concat('000',c1,'$$$'),length(c1) from tchar ;
+----+----------------------------+------------+
| id | concat('000',c1,'$$$') | length(c1) |
+----+----------------------------+------------+
| 1 | 000a$$$ | 1 | <- 删除尾部空格
| 2 | 000 aaaaaaaaaaaaaaaaaaa$$$ | 20 |
| 3 | 000a$$$ | 1 |
| 4 | 000$$$ | 0 | <- 删除尾部空格,结果和id=5一样
| 5 | 000$$$ | 0 |
+----+----------------------------+------------+
2、测试VARCHAR类型
表结构:
CREATE TABLE `tvarchar` (
`id` int(10) unsigned NOT NULL DEFAULT '0',
`c1` varchar(20) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
插入几条记录:
insert into tvarchar values (1, concat('a', repeat(' ',19)));
insert into tvarchar values (2, concat(' ', repeat('a',19)));
insert into tvarchar values (3, 'a');
insert into tvarchar values (4, ' ');
insert into tvarchar values (5, '');
insert into tvarchar values (6, '');
查看存储结构:
(1) INFIMUM record offset:99 heapno:0 ...
(2) SUPREMUM record offset:112 heapno:1 ...
(3) normal record offset:126 heapno:2 ... <- id=1
(4) normal record offset:169 heapno:3 ... <- id=2
(5) normal record offset:212 heapno:4 ... <- id=3
(6) normal record offset:236 heapno:5 ... <- id=4
(7) normal record offset:260 heapno:6 ... <- id=5
(8) normal record offset:283 heapno:7 ... <- id=6
可以看到,几条记录的字节数分别是:43、43、24、24、23、23(最后一条记录和id=5那条记录一样)。
对上面这个结果有点诧异是不是,尤其是id=1的记录(插入的是'a…后面19个空格'),居然也要消耗43字节,这就佐证了上面的结论2。
同样的,id=3和id=4这两条记录都是占用24字节,而id=5和id=6这两条记录都是占用23字节(没有额外存储字符串的字节数,只有id列4个字节)。
再看下读取tvarchar表的结果:
select id,concat('000',c1,'$$$'),length(c1) from tvarchar;
+----+----------------------------+------------+
| id | concat('000',c1,'$$$') | length(c1) |
+----+----------------------------+------------+
| 1 | 000a $$$ | 20 | <- 读取结果中没有删除尾部的空格
| 2 | 000 aaaaaaaaaaaaaaaaaaa$$$ | 20 |
| 3 | 000a$$$ | 1 |
| 4 | 000 $$$ | 1 | <- 读取结果中没有删除此空格
| 5 | 000$$$ | 0 |
| 6 | 000$$$ | 0 |
+----+----------------------------+------------+
总的来说,可以总结成两条结论:
1、从读取的结果来看,CHAR类型列看起来像是在存储时把空格给吃了,但实际上只是在读取时才给吃了(显示层面上把空格删除了)。
2、从读取的结果来看,VARCHAR类型列看起来像是反倒保留了多余的空格,实际上也是只在读取时才恢复这些空格(但实际物理存储时还是会删掉这些空格)。
最后,来看下文档里怎么说的:
When CHAR values are stored, they are right-padded with spaces to the
specified length. 简言之,CHAR列在存储时尾部加空格补齐长度。When CHAR values are retrieved, trailing spaces are removed unless the
PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.
简言之,CHAR列在读取时会去掉尾部空格,除非设置sql_mode值PAD_CHAR_TO_FULL_LENGTH=1。VARCHAR values are not padded when they are stored.
简言之,存VARCHAR时尾部不加空格。Trailing spaces are retained when values are stored and retrieved, in
conformance with standard SQL. 简言之,读取VARCHAR时会显示空格。
以上测试使用的版本及环境:
mysql> select version()\G
...
version(): 8.0.15
mysql> select @@sql_mode\G
...
@@sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
参考文档
11.4.1 The CHAR and VARCHAR Types,https://dev.mysql.com/doc/refman/5.7/en/char.html
来源:https://blog.p2hp.com/archives/6616
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 决策树算法的实现
Facebook的特别之处是什么?
![](https://img.aspxhome.com/file/UploadPic/20088/4/20088413183866s.gif)
使用JavaScript构建JSON格式字符串实现步骤
Python3.7.0 Shell添加清屏快捷键的实现示例
![](https://img.aspxhome.com/file/2023/6/86356_0s.png)
分享一个简单的python读写文件脚本
Python中ConfigParser模块示例详解
python 按照sheet合并多个Excel的示例代码(多个sheet)
![](https://img.aspxhome.com/file/2023/9/130619_0s.png)
PHP getName()函数讲解
![](https://img.aspxhome.com/file/2023/6/55406_0s.png)
12种最常用的网页编程语言简介(值得收藏)
js+CSS实现弹出居中背景半透明div层的方法
使用Kivy将python程序打包为apk文件
![](https://img.aspxhome.com/file/2023/6/123686_0s.png)
Python使用numpy产生正态分布随机数的向量或矩阵操作示例
![](https://img.aspxhome.com/file/2023/5/67925_0s.png)
Python logging模块写入中文出现乱码
ASP网站程序自动升级实现的方法
Python如何爬取b站热门视频并导入Excel
![](https://img.aspxhome.com/file/2023/0/110280_0s.png)
Python实现获取网页内容及自动填表单与登录功能
![](https://img.aspxhome.com/file/2023/6/124936_0s.png)
python 用正则表达式筛选文本信息的实例
python email smtplib模块发送邮件代码实例
![](https://img.aspxhome.com/file/2023/5/133665_0s.jpg)
python实现剪切功能
![](https://img.aspxhome.com/file/2023/4/109864_0s.jpg)