MySQL 8.0 InnoDB Tablespaces之File-per-table tablespaces(单独表空间)

文章目录

  • MySQL 8.0 InnoDB Tablespaces之File-per-table tablespaces(单独表空间)
    • File-per-table tablespaces(单独表空间)
      • 相关变量:innodb_file_per_table
      • 使用TABLESPACE子句指定表空间
        • 变量innodb_file_per_table设置为关闭状态时创建表
        • 指定TABLESPACE:innodb_file_per_table创建表
    • 例题
    • 参考

【免责声明】文章仅供学习交流,观点代表个人,与任何公司无关。
编辑|SQL和数据库技术(ID:SQLplusDB)

MySQL 8.0 OCP (1Z0-908) 考点概要

MySQL 8.0 OCP (1Z0-908) 考点精析-安装与配置考点1:设置系统变量
【MySQL】控制MySQL优化器行为方法之optimizer_switch系统变量
【MySQL】MySQL系统变量(system variables)列表(mysqld --verbose --help的结果例)
【MySQL】MySQL系统变量(system variables)列表(SHOW VARIABLES 的结果例)
MySQL 8.0 OCP (1Z0-908) 考点精析-备份与恢复考点1:MySQL Enterprise Backup概要
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点1:sys.statement_analysis视图
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点2:系统变量的确认
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点3:EXPLAIN ANALYZE
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点4:慢速查询日志(slow query log)
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点5:表连接算法(join algorithm)
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点6:MySQL Enterprise Monitor之Query Analyzer
MySQL 8.0 OCP (1Z0-908) 考点精析-架构考点1:二进制日志文件(Binary log)
MySQL 8.0 OCP (1Z0-908) 考点精析-架构考点5:数据字典(Data Dictionary)
MySQL 8.0 OCP (1Z0-908) 考点精析-架构考点6:InnoDB Tablespaces之系统表空间(System Tablespace)

MySQL 8.0 InnoDB Tablespaces之File-per-table tablespaces(单独表空间)

InnoDB表空间是MySQL中用于存储InnoDB存储引擎表数据和索引的物理文件。
InnoDB Architecture
InnoDB表空间根据用途可以分成多种类型:

  • 数据表空间:
    • System tablespace(系统表空间)
    • File-per-table tablespaces(单独表空间)
    • General tablespaces(一般表空间)
  • Undo 表空间
  • 临时表空间(Temporary table tablespaces)

File-per-table tablespaces(单独表空间)

MySQL中在InnoDB中创建表的时候默认使用File-per-table tablespaces(单独表空间),即创建一个InnoDB 表会创建一个独立的表空间用于保存表的数据、索引以及元数据。
表空间的数据文件会保存在MySQL data目录中 ,并且.ibd文件的命名以表的名称命名(table_name.ibd)。
一个表对应一个表空间一个数据文件。

例:创建一个测试表

mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table test(a int);
Query OK, 0 rows affected (0.02 sec)

OS上会创建对应的.ibd文件。

root@mysql-vm:/var/lib/mysql/testdb# ls -l test.ibd
-rw-r----- 1 mysql mysql 114688 Dec 11 23:00 test.ibd

相关变量:innodb_file_per_table

MySQL使用变量innodb_file_per_table控制创建表的表空间。

  • 当启用时,表会创建在 File-per-table tablespaces(单独表空间)中。
  • 当关闭时,表会创建在System tablespace(系统表空间)中。

innodb_file_per_table变量可以使用SET GLOBAL语句在运行时进行配置,在启动时在命令行中指定,或在选项文件中指定。
当位于File-per-table tablespaces(单独表空间)中的表被truncate 或者drop时,释放的空间将返回给操作系统。
位于系统表空间中的表被truncate 或者drop时,仅释放系统表空间中的空间。系统表空间中的空闲空间可以再次用于InnoDB数据,但不会返回给操作系统。
系统表空间数据文件永远不会缩小。

Command-Line Format–innodb-file-per-table
System Variableinnodb_file_per_table
ScopeGlobal
DynamicYes
SET_VAR Hint AppliesNo
TypeBoolean
Default ValueON

例:

mysql> show variables like 'innodb_file_per_table';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_file_per_table | ON    |
+-----------------------+-------+
1 row in set (0.01 sec)

mysql>

使用TABLESPACE子句指定表空间

当变量innodb_file_per_table设置为关闭状态时,默认创建的表会创建在system表空间中。

变量innodb_file_per_table设置为关闭状态时创建表

例:

mysql> use testdb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> set global innodb_file_per_table='OFF';
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'innodb_file_per_table';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_file_per_table | OFF   |
+-----------------------+-------+
1 row in set (0.00 sec)

mysql> create table test_file_pertable(a int);
Query OK, 0 rows affected (0.03 sec)

mysql> select
    ->     name as 'table_name',
    ->     space_type
    -> from
    ->     information_schema.innodb_tables
    -> where
    ->     name like '%test_file_pertable%' \g
+---------------------------+------------+
| table_name                | space_type |
+---------------------------+------------+
| testdb/test_file_pertable | System     |
+---------------------------+------------+
1 row in set (0.00 sec)

指定TABLESPACE:innodb_file_per_table创建表

当变量innodb_file_per_table设置为关闭状态时,也可以通过使用TABLESPACE子句指定使用单独表空间(innodb_file_per_table)。

例:

mysql> show variables like  'innodb_file_per_table';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_file_per_table | OFF   |
+-----------------------+-------+
1 row in set (0.01 sec)

mysql> create table test_file_pertable_tablespace(a int)
    -> tablespace =innodb_file_per_table;
Query OK, 0 rows affected (0.04 sec)

mysql> select
    ->     name as 'table_name',
    ->     space_type
    -> from
    ->     information_schema.innodb_tables
    -> where
    ->     name like '%test_file_pertable_tablespace%' \g
+--------------------------------------+------------+
| table_name                           | space_type |
+--------------------------------------+------------+
| testdb/test_file_pertable_tablespace | Single     |
+--------------------------------------+------------+
1 row in set (0.01 sec)

mysql>

通过information_schema.innodb_tables表可以看到创建表的表空间类型为Single(单独表空间),并且创建了表同名的ibd文件。

root@mysql-vm:/var/lib/mysql/testdb# ls -l test_file_pertable_tablespace.ibd
-rw-r----- 1 mysql mysql 114688 Dec 20 12:56 test_file_pertable_tablespace.ibd
root@mysql-vm:/var/lib/mysql/testdb#

例题

Choose two.
Your MySQL server was upgraded from an earlier major version.
The sales database contains three tables, one of which is the transactions table, which has 4million rows.
You are running low on disk space on the datadir partition and begin to investigate.
Examine these commands and output:

在这里插入代码片

Which two statements are true?
A) The transactions table was created with innodb_file_per_table=OFF.
B) Truncating the sales and leads table will free up disk space.
C) Executing SET GLOBAL innodb_row_format=COMPRESSED and then ALTER TABLE transactions will free up disk space.
D) Executing ALTER TABLE transactions will enable you to free up disk space.
E) Truncating the transactions table will free up the most disk space.

答案 AB

例题解析:
1.innodb_file_per_table=on时,表会创建在 File-per-table tablespaces(单独表空间)中
MySQL中在InnoDB中创建表的时候默认使用File-per-table tablespaces(单独表空间),即创建一个InnoDB 表会创建一个独立的表空间用于保存表的数据、索引以及元数据。
表空间的数据文件会保存在MySQL data目录中 ,并且.ibd文件的命名以表的名称命名(table_name.ibd)。
一个表对应一个表空间一个数据文件。
2. 单独表空间中的表被truncate 或者drop时,会释放的空间将返回给操作系统。

参考

15.6.3.2 File-Per-Table Tablespaces
https://dev.mysql.com/doc/refman/8.0/en/innodb-file-per-table-tablespaces.html

  • 24
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SQLplusDB

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值