mysql增加了表格字段,结果表格数据体积减小了?
黄舟
黄舟 2017-04-17 16:17:20
[MySQL讨论组]

发现了两个需要新存储的字段,就通过alter add添加了两个字段,一个int, 一个float,结果发现变更完之后表格占用的体积反而减少了。

变更前:43个字段,14个索引字段,8141516 kb;
变更后:45个字段,14个索引字段,8120649 kb;
数据表大约226w条数据,myisam引擎,新添加的两个字段里面尚未写入数据,但减少了大约20M。

是因为在做数据表变更时同时做了优化、压缩之类的操作吗?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(1)
PHPz

alter table在大部分情况下,会对原来的表生成一个临时的副本(临时表)。然后更新会进行到这个临时表里,创建一个新的表,删除原来的表。所以可以通过alter table 来优化表空间,修复操作产生的碎片空间。所以表空间变小了。我描述的可能不是很好,官方文档原话是这样子的

In most cases, ALTER TABLE makes a temporary copy of the original table. MySQL waits for other operations that are modifying the table, then proceeds. It incorporates the alteration into the copy, deletes the original table, and renames the new one. While ALTER TABLE is executing, the original table is readable by other sessions (with the exception noted shortly). Updates and writes to the table that begin after the ALTER TABLE operation begins are stalled until the new table is ready, then are automatically redirected to the new table without any failed updates. The temporary copy of the original table is created in the database directory of the new table. This can differ from the database directory of the original table for ALTER TABLE operations that rename the table to a different database.

具体官方文档链接可以参考
https://dev.mysql.com/doc/ref...

PS: 可以看文档下面的comments,有人说到

You can use Alter Table to optimise a table without locking out
selects (only writes), by altering a column to be the same as it's
current definition. This is better than using repair table which
obtains a read/write lock.

截个图

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号