温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL5.6开始可以使用独立表空间, innodb_file_per_table=1

发布时间:2020-08-07 20:48:48 来源:ITPUB博客 阅读:156 作者:shytodear 栏目:MySQL数据库
MySQL5.6开始可以使用独立表空间:
MySQL5.6 
innodb_file_per_table=1 #使用独立表空间,动态参数。(5.6默认OFF,5.7默认ON)


1、drop/truncate table方式操作表空间能自动回收(磁盘空间)

1)、创建procedure,循环insert一定量数据
##use test
##drop procedure pro1;

DELIMITER //
create procedure pro1()
begin
declare i int;
set i=1;
while i<100000 do
    insert into test.cc(id,name) values(i, "aa");
    set i=i+1;
end while;
end;//

2)、调用procedure :
mysql> call pro1();


3)、查看表大小、数据量:
select table_name, (data_length+index_length)/1024/1024 as total_mb, table_rows
   from information_schema.tables where table_schema='test' and table_name='CC';

+------------+------------+------------+
| table_name | total_mb   | table_rows |
+------------+------------+------------+
| cc         | 3.51562500 |     100246 |
+------------+------------+------------+
1 row in set (0.31 sec)

4)、truncate清表:
mysql> truncate table test.cc;
Query OK, 0 rows affected (0.73 sec)

5)、再次查看表空间已经回收:

cc.ibd 由  11264KB 回收到96KB 。

mysql> select table_name, (data_length+index_length)/1024/1024 as total_mb, table_rows
    -> from information_schema.tables where table_schema='test' and table_name='CC';
+------------+------------+------------+
| table_name | total_mb   | table_rows |
+------------+------------+------------+
| cc         | 0.01562500 |          0 |
+------------+------------+------------+
1 row in set (0.00 sec)

mysql>

mysql> select version();
+------------+
| version()  |
+------------+
| 5.7.11-log |
+------------+
1 row in set (0.08 sec)

mysql>

注:drop table test.cc ; 物理文件cc.ibd也会同时被删除。


2、独立表空间下,可以自定义表的存储位置,(有时将部分热表放在不同的磁盘可有效地提升IO性能)
create table test(id int) data directory='c:/software';
create table test1(id int,name varchar(20),primary key (id)) data directory='c:/software';

3、独立表空间下,可以回收表空间碎片(比如一个非常大的delete操作之后释放的空间)

1)创建测试表
DELIMITER //
create procedure pro_test1()
begin
declare i int;
set i=1;
while i<10000 do
    insert into test.test1(id,name) values(i, "aa");
    set i=i+1;
end while;
end;//

##call pro_test1();


表大小:test1.ibd   368KB

2)delete后表大小:
mysql> delete from test1;
test1.ibd   384KB

3)回收表空间
mysql> alter table test1 engine=innodb; 
test1.ibd   96KB

mysql> select table_name, (data_length+index_length)/1024/1024 as total_mb, table_rows
   from information_schema.tables where table_schema='test' and table_name='TEST1';

+------------+------------+------------+
| table_name | total_mb   | table_rows |
+------------+------------+------------+
| test1      | 0.01562500 |          0 |
+------------+------------+------------+
1 row in set (0.00 sec)


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI