1、查看当前mysql使用的是什么方式
show variables like 'innodb_file_per_table';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_file_per_table | ON |
+-----------------------+-------+
1 row in set
2、这个参数值允许动态修改,修改后,只是对新建的表有效,所以建议一开始就在配置里面设置为独立表空间
innodb_file_per_table=1 为使用独立表空间
innodb_file_per_table=0 为使用共享表空间
参数设置命令:
set global innodb_file_per_table =1;
3、该参数的默认值与MySQL版本有关
小于等于MySQL5.6.5默认OFF
大于等于MySQL5.6.6默认ON
4、个人建议使用独立表空间
共享表空间,无法回收空间,特别是某些需要经常删除的表,更不建议使用。
独立表空间,当对表数据有大量的删除处理后,我们可以使用
optimize table tablename;或者alter table tablename engine=innodb;来进行回收。
备注: 注意表的大小,而系统空间是否足够,回收表的时候,会导致大量的磁盘空间被占用。
5、测试,实验版本 mysql5.7
#创建一个数据库:
create database test default character set utf8mb4 collate utf8mb4_unicode_ci;
#创建一张表:
CREATE TABLE `article` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`content` text COLLATE utf8mb4_unicode_ci,
`created_at` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='文章表';
#查看表大小
select data_length,index_length from information_schema.tables where table_schema='test' and table_name = 'article';
+-------------+--------------+
| data_length | index_length |
+-------------+--------------+
| 16384 | 0 |
+-------------+--------------+
1 row in set (0.00 sec)
或者可以使用查看:
show table status like 'article';
插入数据:
#执行一次
insert into article(title,content,created_at)values('标题','内容',123);
#执行十次:
insert into article(title,content,created_at) select title,content,created_at from article;
#再次查看表大小:
select data_length,index_length from information_schema.tables where table_schema='test' and table_name = 'article';
+-------------+--------------+
| data_length | index_length |
+-------------+--------------+
| 81920 | 0 |
+-------------+--------------+
1 row in set (0.00 sec)
#删除数据
delete from article;
#查看数据,依然一样大小
select data_length,index_length from information_schema.tables where table_schema='test' and table_name = 'article';
+-------------+--------------+
| data_length | index_length |
+-------------+--------------+
| 81920 | 0 |
+-------------+--------------+
1 row in set (0.00 sec)
#优化表,表变小了:
optimize table test.article;
select data_length,index_length from information_schema.tables where table_schema='test' and table_name = 'article';
+-------------+--------------+
| data_length | index_length |
+-------------+--------------+
| 16384 | 0 |
+-------------+--------------+
1 row in set (0.00 sec)
参考学习的相关文章: