创建表之后的MySQL表的意外尺寸增加,然后插入

发布于 2025-02-09 14:51:33 字数 1036 浏览 1 评论 0原文

我正在尝试使用以下查询来复制MySQL(版本5.7.38-1)中的表格:

CREATE TABLE dest LIKE src;
INSERT INTO dest SELECT * FROM src;

创建表DENT并填充表SRC中的记录。到目前为止,一切都很好。您希望这两张桌子的尺寸大致相同。但是Table DEST具有646m,而Table SRC只有134m。创建步骤之后,表格为48K,或多或少是预期的。

引擎是InnoDB,默认行格式是动态的,并且压缩正在打开。

我已经执行了以下操作,以查看它是否会有所帮助,但无济于事:

ALTER TABLE dest ROW_FORMAT=COMPRESSED;
OPTIMIZE TABLE dest;

这是show create table src

CREATE TABLE `src` (
  `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `post_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `meta_value` longtext COLLATE utf8mb4_unicode_ci,
  PRIMARY KEY (`meta_id`),
  KEY `post_id` (`post_id`),
  KEY `meta_key` (`meta_key`(191))
) ENGINE=InnoDB AUTO_INCREMENT=6046271 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

我知道MySQL版本已经过时了,但是更改了我的控制范围。

两个问题:

  1. 这种意外行为的原因是什么?
  2. 什么解决方案是使餐桌更小的解决方案?

感谢您的见解。

I am trying to copy a table in mysql (version 5.7.38-1) using the following queries:

CREATE TABLE dest LIKE src;
INSERT INTO dest SELECT * FROM src;

Table dest is created and filled with records from Table src. So far, so good. You would expect the two tables to have roughly the same size. But Table dest has 646M, whereas Table src only has 134M. After the create-step, Table dest is 48K, more or less as expected.

Engine is InnoDB, default row format is dynamic and compression is on.

I have executed the following to see if it would help but to no avail:

ALTER TABLE dest ROW_FORMAT=COMPRESSED;
OPTIMIZE TABLE dest;

And this is SHOW CREATE TABLE src:

CREATE TABLE `src` (
  `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `post_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `meta_value` longtext COLLATE utf8mb4_unicode_ci,
  PRIMARY KEY (`meta_id`),
  KEY `post_id` (`post_id`),
  KEY `meta_key` (`meta_key`(191))
) ENGINE=InnoDB AUTO_INCREMENT=6046271 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

I am aware that the mysql version is dated but changing that is outside my scope of control.

Two questions:

  1. What is the reason for this unexpected behavior?
  2. What is the solution to make Table dest smaller?

Thanks for your insights.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请你别敷衍 2025-02-16 14:51:33

各种可能性。最有可能的是索引。

什么引擎?压缩了吗?哪种行格式?

请提供显示创建表Src

插入...选择...一次将行馈入表格一(但比一个insert每行要快得多)。如果引擎为Innodb,则大概是src行在主键 order中。插入dst的最佳顺序也是该顺序。因此,我希望数据的btree有效地“碎片片”。

次要索引是另一回事。他们可能会有效订购或可能不会有效。 “更改缓冲区”可能会或可能不会补偿订购。每个二级索引的最终btree可能会“碎片碎片”。

哪个版本的MySQL/Mariadb?我可能有一个工具可以更深入地研究这个问题。

Various possibilities. The most likely is the indexes.

What engine? Was compression on? What row format?

Please provide SHOW CREATE TABLE src.

INSERT ... SELECT ... will feed the rows into the table one at a time (but a lot faster than one INSERT statement per row). If the Engine is InnoDB, then presumably the src rows are in PRIMARY KEY order. And the optimal order for inserting to dst is also that order. So I would expect the data's BTree to be effectively 'defragmented'.

Secondary indexes are another matter. They may or may not be efficiently ordered. And the "change buffer" may or may not compensate for the ordering. The resulting BTree for each secondary index may or may not be 'defragmented'.

What version of mysql/mariadb? I may have a tool to look deeper into the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文