当我看到 db_1 的大小时,它列出的大小只有 4k。但是当我显示 db_1 的表状态时,我看到根据数据长度,总大小约为 3.7 GB。为什么会有这种差异?为什么 ibdata1 这么大?
drwx------ 2 mysql mysql 4.0K Jun 3 08:52 db_1
-rw-r-xr-x 1 mysql mysql 22G Jun 3 08:51 ibdata1
drwx------ 2 mysql mysql 4.0K Apr 30 22:30 my_DB
drwxr-xr-x 2 mysql dba 8.0K May 1 12:44 mysql
-rw-r-xr-x 1 mysql root 4.4K Jun 1 14:33
-rw-rw---- 1 mysql mysql 5 May 17 18:19
-rw-r-xr-x 1 mysql mysql 51K Jun 3 08:41
drwxr-xr-x 2 mysql dba 4.0K Apr 30 22:29 performance_schema
----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+------------------------------------------------+--------+---------+------------+----------+----------------+-------------+-----------------+--------------+-------------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
| xxxxxxxxxxxxxxxxxxxxxx | InnoDB | 10 | Compact | 117 | 140 | 16384 | 0 | 0 | 17825792000 | NULL | 2012-05-01 13:24:34 | NULL | NULL | latin1_swedish_ci | NULL | | |
| xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx| InnoDB | 10 | Compact | 466203 | 248 | 116015104 | 0 | 0 | 17825792000 | NULL | 2012-05-01 13:31:28 | NULL | NULL | latin1_swedish_ci | NULL | | |
| xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | InnoDB | 10 | Compact | 1028265 | 111 | 114147328 | 0 | 0 | 17825792000 | NULL | 2012-05-01 13:32:36 | NULL | NULL | latin1_swedish_ci | NULL | | |
| xxxxxxxxxxxxxxxxxxxxxxxx | InnoDB | 10 | Compact | 1353685 | 98 | 133922816 | 0 | 0 | 17825792000 | NULL | 2012-05-01 13:32:49 | NULL | NULL | latin1_swedish_ci | NULL | | |
| xxxxxxxxxxxxxxxxxx | InnoDB | 10 | Compact | 50546 | 52 | 2637824 | 0 | 0 | 17825792000 | NULL | 2012-05-01 13:04:18 | NULL | NULL | latin1_swedish_ci | NULL | | |
| xxxxxxxxxxxxxxxxxxxxxx | InnoDB | 10 | Compact | 44281 | 83 | 3686400 | 0 | 0 | 17825792000 | NULL | 2012-05-01 13:19:25 | NULL | NULL | latin1_swedish_ci | NULL | | |
| xxxxxxxxxxxxxxxxxxxxxxxxx | InnoDB | 10 | Compact | 5841698 | 124 | 729808896 | 0 | 163397632 | 17825792000 | NULL | 2012-05-01 13:17:29 | NULL | NULL | latin1_swedish_ci | NULL | | |
| xxxxxxxxxxxxxxxxxxxxxxxx | InnoDB | 10 | Compact | 52327792 | 60 | 3171942400 | 0 | 0 | 17825792000 | NULL | 2012-05-03 17:09:41 | NULL | NULL | latin1_swedish_ci | NULL | | |
+------------------------------------------------+--------+---------+------------+----------+----------------+-------------+-----------------+--------------+-------------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
答案1
这是一个非常常见的问题,已经被问过很多次了。
这是因为您将所有表都存储在一个 InnoDB 文件 (ibdata1) 中。删除数据后,此文件永远不会缩小。因此,您曾经有 22GB 的数据存储在所有 InnoDB 表中,即使现在可能没有那么多。
要解决这个问题,您需要删除所有 InnoDB 表,关闭 MySQL,删除该文件,重新启动 MySQL,然后重新导入这些表。
您也可以使用 MySQL 设置innodb_file_per_table
将每个 InnoDB 表拆分成各自的文件。文件仍不会自动缩小,但您可以使用 执行清理alter table ... type=innodb
。