黄石网站建设维护,同一个域名网站做301,徐州集团网站建设方案,网站建设营销话术转自#xff1a; https://yq.aliyun.com/articles/600183
SDI是Serialized Dictionary Information的缩写#xff0c;是MySQL8.0重新设计数据词典后引入的新产物。我们知道MySQL8.0开始已经统一使用InnoDB存储引擎来存储表的元数据信息#xff0c;但对于非InnoDB引擎#…转自 https://yq.aliyun.com/articles/600183
SDI是Serialized Dictionary Information的缩写是MySQL8.0重新设计数据词典后引入的新产物。我们知道MySQL8.0开始已经统一使用InnoDB存储引擎来存储表的元数据信息但对于非InnoDB引擎MySQL提供了另外一中可读的文件格式来描述表的元数据信息在磁盘上以 $tbname.sdi的命名存储在数据库目录下。
你可以基于sdi文件来对非事务引擎表进行导出导入操作具体参阅官方文档
而对于InnoDB表没有创建sdi文件而是将sdi信息冗余存储到表空间中临时表空间和undo表空间除外。
既然已经有了新的数据词典系统为什么还需要冗余的sdi信息呢这主要从几个方面考虑
当数据词典损坏时实例可能拒绝启动在这种情况下我们可以通过冗余的sdi信息将数据拷贝到另外一个实例上并进行数据重建可以离线的查看表的定义可以基于此实现简单的数据转储MySQL Cluster/Ndb也可能依赖信息进行元数据同步
官方提供了一个工具叫做ibd2sdi在安装目录下可以找到可以离线的将ibd文件中的冗余存储的sdi信息提取出来并以json的格式输出到终端。
ibd2sdi工具的使用文档
相关worklog: WL#7069
下面简单的试玩下
roottest 02:59:12create table t1 (a int primary key, b char(10));
Query OK, 0 rows affected (0.02 sec)
通过ibd2sdi查看ibd文件
$bin/ibd2sdi data/test/t1.ibd
// 默认情况下打印所有信息
// 包含所有的列索引的各个属性也可以查询部分信息$bin/ibd2sdi --skip-data data/test/t1.ibd
[ibd2sdi
,
{type: 1,id: 701
}
,
{type: 2,id: 235
}
]$bin/ibd2sdi --id 235 data/test/t1.ibd
[ibd2sdi
,
{type: 2,id: 235,object:{mysqld_version_id: 80011,dd_version: 80011,sdi_version: 1,dd_object_type: Tablespace,dd_object: {name: test/t1,comment: ,options: ,se_private_data: flags16417;id212;server_version80011;space_version1;,engine: InnoDB,files: [{ordinal_position: 1,filename: ./test/t1.ibd,se_private_data: id212;}]}
}
}
]
当然还有很多其他的选项(ib2sdi --help)感兴趣的可以自行把玩。
首先ibd文件的第一个page中标记了是否存在sdi信息:FSP_FLAGS_POS_SDI如果是从老版本(例如5.7) 物理恢复过来的数据该标记位是未设置的。
在建表时sdi相关堆栈如下
ha_innobase::create
|-- innobase_basic_ddl::create_impl|-- create_table_info_t::create_table|-- create_table_info_t::create_table_def|-- row_create_table_for_mysql|-- dict_build_table_def|-- dict_build_tablespace_for_table|-- btr_sdi_create_index
btr_sdi_create_index:
从代码来看,sdi index也是按照一个普通btree的标准函数(btr_create)来创建的但其对应的page类型为FIL_PAGE_SDI 内存中有一个和tablespace_id对应的dict_table_t对象称作sdi table(dict_sdi_get_table), 表上包含个列 typeidcompressed_lenuncompressed_lendata(blobsdi table的table_id从tablespace id中生成(dict_sdi_get_table_id)sdi table上包含一个索引索引列为(type, id), ref: dict_sdi_create_idx_in_memsdi pindex的root page no及sdi版本号被写到表空间的第一个page中(fsp_sdi_write_root_to_page)另外在之前版本中ibd文件的初始化大小为个page而到了8.0版本变成了7个page(FIL_IBD_FILE_INITIAL_SIZE)包括
0: file space header
1: insert buffer bitmap
2: inode page
3: sdi index page
4: index page
5: freshly allocated page
6: freshly allocated page
在创建完table对象后需要更新全局数据词典(create_table_info_t::create_table_update_global_dd)
写入tablespace信息到sdi中
innobase_basic_ddl::create_impl
|-- create_table_info_t::create_table_update_global_dd|-- dd_create_implicit_tablespace|-- create_dd_tablespace|-- dd::cache::Dictionary_client::store|-- dd::cache::Storage_adapter::store|-- dd::sdi::store|-- dd::sdi_tablespace::store_tsp_sdi|-- dict_sdi_set
id220
写入table信息到sdi中包含了完整表的定义每个列的属性信息等
ha_create_table
|--dd::cache::Dictionary_client::updatedd::Table |--dd::cache::Storage_adapter::storedd::Table|--dd::sdi::store|--dd::(anonymous namespace)::with_schemalong long unsigned int, dd::sdi::store|-- operator() |-- dd::sdi_tablespace::store_tbl_sdi|-- apply_to_tablespaces //sdi_tablespace.cc:140|-- ...|-- dict_sdi_set
Server层提供的sdi接口
[$/MySQL_8.0/sql/dd/impl]
$ls * | grep sdi
sdi_api.cc
sdi.cc
sdi_file.cc
sdi.h
sdi_impl.h
sdi_tablespace.cc
sdi_tablespace.h
sdi_utils.h
构建sdi序列化词典信息的入口函数: dd::serialize, 这里会产生一个字符串存储sdi信息调用innodb接口函数dict_sdi_set将数据插入到其sdi index中接口使用innodb api/api0api.cc之前只由memcached使用实际存储的数据是经过压缩的压缩算法为zlib(Sdi_Compressor)向ibd中写入记录(ib_sdi_set)如果存在duplicate key则更新记录之前介绍过sdi index中存在lob字段其外部存储页的类型为FIL_PAGE_SDI_BLOB或者FIL_PAGE_SDI_ZBLOB当使用general tablespace时sdi index中会存储多条数据