MySQL innodb存储引擎的共享表空间、独立表空间

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)

参考学习的相关文章:

https://blog.csdn.net/jesseyoung/article/details/42236615

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值