搜索
首页数据库mysql教程InnoDB实现独立表空间多数据文件 (InnoDB multiple datafiles pe

我们知道,在Oracle中,每个表空间都可以由很多文件组成,这样文件的IO就可以分散在很多存储路径上。虽然MySQL的服务器一般来说不会配置多路径存储,但是,很多老式文件系统(例如EXT3)对大文件的IO操作支持不好,性能很差,所以对MySQL/InnoDB来说,把数据文

我们知道,在Oracle中,每个表空间都可以由很多文件组成,这样文件的IO就可以分散在很多存储路径上。虽然MySQL的服务器一般来说不会配置多路径存储,但是,很多老式文件系统(例如EXT3)对大文件的IO操作支持不好,性能很差,所以对MySQL/InnoDB来说,把数据文件大小控制在比较小的范围,也是有好处的。

As we know that Oracle can let one tablespace contains many datafiles, so file IO can distribute to multiple storage paths. Most of MySQL servers will not use multiple storage paths, but many old filesystems can’t support large files well, if datafiles too larger, performance will be lower, such as EXT3. So keeping MySQL/InnoDB datafiles size in a relatively small range is beneficial, too.

InnoDB在共享表空间模式下,是支持多文件的,用innodb_data_file_path选项可以配置:

InnoDB supports multiple datafiles in the shared tablespace mode, and we can configure it with innodb_data_file_path:

innodb_data_file_path = /disk1/ibdata1:2G;/disk2/ibdata2:2G:autoextend

这样配置就把数据文件分散在了disk1和disk2两个路径下,第一个文件固定2G大小,第二个文件初始化2G,可以自增长。

Configuring innodb_data_file_path like this, InnoDB can distribute the two datafiles to disk1 & disk2. The first file is fiexed size, 2GB. And the second file is auto extened, initial size id 2GB.

但是如果设置了 innodb_file_per_table 选项,每张表都会有一个独立的表空间文件,就不能再对每个独立表空间使用多数据文件了。但是,即使每张表分配一个独立的文件,还是可能有某些表变得非常大,例如我们就有几百GB的表,在XFS文件系统上这还没什么问题,有些系统为了“安全”依然使用EXT3,大文件的操作性能就堪忧了。

But if innodb_file_per_table = TRUE, each tables will have their single tablespace datafile, and innodb_data_file_path only used for system-tablespace, single-tablespace can’t use mutiple datafiles. Even thought each tables will have one single datafile, file maybe become very large, too.

当然,可以通过分库分表分区来让数据文件变小,对于大部分小公司都没有中间件来完成分库分表的工作,而大表随处可见,业务变化快,用分区也不合理,因此,通过为独立表空间增加多数据文件的功能,是很好的选择。

Of course, we can split databases / tables, or use partition, it can let datafiles become small. But most of small companies haven’t middleware to split these, and they also have many big tables. So it’s best for them to use mutile datafiles per single-tablespace feature.

如何通过尽可能少的改动,来为InnoDB独立表空间也增加多数据文件的功能呢,经过一段时间调研,可以发现,大多数地方,InnoDB并没有用特别的方式来判断是独立表空间还是共享表空间,并且表空间描述符并没有因共享表空间还是独立表空间而有差异,都是使用 fil_space_t,并且其中 fil_space_t->chain 就是记录从属于表空间的所有文件,用 fil_node_t 描述。

How to implement multiple datafiles per single-tablespace feature with modifying source code as little as possible? I found something useful through research, InnoDB haven’t do many special judge for shared/single-tablesapce, and tablespace descriptor is the same for them (fil_space_t). And fil_space_t->chain (fil_node_t) is the list of the files belong to this tablespace.

尤其当我看到这个注释时:

Especially when I saw this comment:

  /* TODO: The following code must change when InnoDB supports
  multiple datafiles per tablespace. */

我觉得InnoDB团队在开发时,也已经考虑到了未来需要增加表空间多文件的支持,更让我确信这是可以实现的。

I think InnoDB team want to do it, too. And they are already do enough preparation when they code. So I’m sure I can implement this feature.

因此基于5.6的源码树修改代码测试,我觉得如下思路是靠谱的,正按着这个方案重新整理代码:

And then I try to modify code on MySQL 5.6 source code, I found a practical way, I’m coding with this design:

用户接口 (User Interface):

CREATE TABLE语法新增两个参数:DATAFILE_INITIAL_SIZE, DATAFILE_NUM,分别表示数据文件初始大小和数据文件数量。

I added two options in CREATE TABLE syntax: DATAFILE_INITIAL_SIZE & DATAFILE_NUM. They represent the initial size of the data files and the number of data files.

CREATE TABLE table_name (...) ENGINE=InnoDB 
  DATAFILE_INITIAL_SIZE=1000000, DATAFILE_NUM=100;

这样就会建100个包含1000000个页面的文件,命名方式采用 “table_name#num.ibd”,都建在默认数据目录下,最多允许初始化255个文件,每个文件都是固定大小,如果还需要增加文件,需要使用ALTER TABLESPACE命令。

This SQL will let MySQL create a table with 100 datafiles, and each datafiles have 1000000 pages. The auto created datafiles named “table_name#num.ibd” in the default datadir. Allowed to contain up to 255 data files, each datafiles are fixed size. If you want to add datafile after created table, you need to use ALTER TABLESPACE command.

ALTER TABLESPACE `db_name/table_name` 
  ADD DATAFILE \'/diskN/table_name#256\' 
  INITIAL_SIZE = 5000 AUTOEXTEND_SIZE=1000 ENGINE=InnoDB;

这个命令会为db_name下的table_name表增加一个数据文件,位置在”/diskN/table_name#256.ibd”(后缀自动加),初始大小为5000个页面,每次自动扩展1000个页面。

This SQL will add a datafile for db_name.tablename, datafile path is “/diskN/table_name#256.ibd” (suffix .ibd is added automatically), initial size is 5000 pages, each autoextend operation will extend 1000 pages.

设计细节 (Details):

1. 在I_S表中的tables表增加data_file_path字段,用于展示表的数据文件位置和大小,类似innodb_data_file_path中共享表空间的记录方式。

1. Adding a column named “data_file_path” on “I_S.tables” table to record the datafiles path and size, like “innodb_data_file_path” option.

2. 在数据目录下,增加table_name.dbf文件,为每张表持久化类似innodb_data_file_path字段的数据文件路径信息。

2. Adding a “table_name.dbf” file for recording datafiles information in datadir. The format like “innodb_data_file_path” option.

3. 在 fil_space_t 结构体中增加三个字段,跟InnoDB全局变量中定义的含义一样,分别用于记录属于表空间的数据文件数量,数据文件名,数据文件大小。

3. Adding 3 variables in fil_space_t, the meaning like InnoDB global variables “srv_n_data_files, srv_data_file_names, srv_data_file_sizes”, but they for each tablespace here.

  ulint   n_data_files;    /* The number of datafiles */
  char**  data_file_names; /* Every datafiles\' name */
  ulint*  data_file_sizes; /* Every datafiles\' size */

4. 增加 srv_ibd_file_initial_size 全局变量,默认等于 FIL_IBD_FILE_INITIAL_SIZE,建表时如果设置了DATAFILE_INITIAL_SIZE选项,并且这个选项 > FIL_IBD_FILE_INITIAL_SIZE,则创建表时用 srv_ibd_file_initial_size 作为初始化大小,这样某些已知会很大的表可以预先扩展,避免未来高速写入时出现扩展问题。

Adding “srv_ibd_file_initial_size” global variable. Its default value is FIL_IBD_FILE_INITIAL_SIZE. If you set DATAFILE_INITIAL_SIZE on “CREATE TABLE”, and the value > FIL_IBD_FILE_INITIAL_SIZE,  then table datafile initial size will set to srv_ibd_file_initial_size. So if you know a table will be very large, you can set this option to pre-extend datafile size, it can avoid extend datafile operation when insert heavy workload.

5. 增加 fil_create_new_datafile_for_single_table_tablesapce() 函数,增加新的数据文件时调用这个函数,会用 os_file_create() 来创建新文件,并用 os_file_set_size() 设置大小,然后用 fil_node_create() 创建node加入 fil_space_t->chain,并更新 fil_space_t->n_data_files/data_file_names/data_file_sizes 三个变量。

Adding fil_create_new_datafile_for_single_table_tablesapce() function, it can add a new datafile for single-tablespace. It will call os_file_create() to create new file, and call  os_file_set_size() to set size, and then call fil_node_create() to create a “node”, this “node” will add to fil_space_t->chain. fil_space_t->n_data_files/data_file_names/data_file_sizes will be updated in the same time.

6. InnoDB启动时在 open_or_create_data_files() 函数中增加检查步骤,查看是否有 table_name.dbf 文件,如果有则读取其中字串,复用共享表空间的处理代码,将解析结果存入表空间结构体 fil_space_t->n_data_files/data_file_names/data_file_sizes。

Adding some process in InnoDB startup function, open_or_create_data_files(). I will check if “table_name.dbf” file is existed, if it’s existed, I will read the string from it. I will use the code that parse “innodb_data_file_path” string, and storing the result to fil_space_t->n_data_files/data_file_names/data_file_sizes.

最终代码很快就会发布。

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
MySQL的许可与其他数据库系统相比如何?MySQL的许可与其他数据库系统相比如何?Apr 25, 2025 am 12:26 AM

MySQL使用的是GPL许可证。1)GPL许可证允许自由使用、修改和分发MySQL,但修改后的分发需遵循GPL。2)商业许可证可避免公开修改,适合需要保密的商业应用。

您什么时候选择InnoDB而不是Myisam,反之亦然?您什么时候选择InnoDB而不是Myisam,反之亦然?Apr 25, 2025 am 12:22 AM

选择InnoDB而不是MyISAM的情况包括:1)需要事务支持,2)高并发环境,3)需要高数据一致性;反之,选择MyISAM的情况包括:1)主要是读操作,2)不需要事务支持。InnoDB适合需要高数据一致性和事务处理的应用,如电商平台,而MyISAM适合读密集型且无需事务的应用,如博客系统。

在MySQL中解释外键的目的。在MySQL中解释外键的目的。Apr 25, 2025 am 12:17 AM

在MySQL中,外键的作用是建立表与表之间的关系,确保数据的一致性和完整性。外键通过引用完整性检查和级联操作维护数据的有效性,使用时需注意性能优化和避免常见错误。

MySQL中有哪些不同类型的索引?MySQL中有哪些不同类型的索引?Apr 25, 2025 am 12:12 AM

MySQL中有四种主要的索引类型:B-Tree索引、哈希索引、全文索引和空间索引。1.B-Tree索引适用于范围查询、排序和分组,适合在employees表的name列上创建。2.哈希索引适用于等值查询,适合在MEMORY存储引擎的hash_table表的id列上创建。3.全文索引用于文本搜索,适合在articles表的content列上创建。4.空间索引用于地理空间查询,适合在locations表的geom列上创建。

您如何在MySQL中创建索引?您如何在MySQL中创建索引?Apr 25, 2025 am 12:06 AM

toCreateAnIndexinMysql,usethecReateIndexStatement.1)forasingLecolumn,使用“ createIndexIdx_lastNameEnemployees(lastName); 2)foracompositeIndex,使用“ createIndexIndexIndexIndexIndexDx_nameOmplayees(lastName,firstName,firstName);” 3)forauniqe instex,creationexexexexex,

MySQL与Sqlite有何不同?MySQL与Sqlite有何不同?Apr 24, 2025 am 12:12 AM

MySQL和SQLite的主要区别在于设计理念和使用场景:1.MySQL适用于大型应用和企业级解决方案,支持高性能和高并发;2.SQLite适合移动应用和桌面软件,轻量级且易于嵌入。

MySQL中的索引是什么?它们如何提高性能?MySQL中的索引是什么?它们如何提高性能?Apr 24, 2025 am 12:09 AM

MySQL中的索引是数据库表中一列或多列的有序结构,用于加速数据检索。1)索引通过减少扫描数据量提升查询速度。2)B-Tree索引利用平衡树结构,适合范围查询和排序。3)创建索引使用CREATEINDEX语句,如CREATEINDEXidx_customer_idONorders(customer_id)。4)复合索引可优化多列查询,如CREATEINDEXidx_customer_orderONorders(customer_id,order_date)。5)使用EXPLAIN分析查询计划,避

说明如何使用MySQL中的交易来确保数据一致性。说明如何使用MySQL中的交易来确保数据一致性。Apr 24, 2025 am 12:09 AM

在MySQL中使用事务可以确保数据一致性。1)通过STARTTRANSACTION开始事务,执行SQL操作后用COMMIT提交或ROLLBACK回滚。2)使用SAVEPOINT可以设置保存点,允许部分回滚。3)性能优化建议包括缩短事务时间、避免大规模查询和合理使用隔离级别。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境