InnoDB数据字典--字典表加载

1、介绍

在InnoDB启动时,如果是新建数据库则需初始化库,需要创建字典管理的相关信息。函数innobase_start_or_create_for_mysql调用dict_create完成此功能。即创建数据字典,因为InnoDB系统表的个数结构固定,所以初始化库的时候只需要创建这几个表的B+树即可并将B+树的根页号存放到固定位置。对于B+树,只要找到根页面,就可以从根页面开始检索数据。相关系统表(即上一节讲到的4个系统表)在InnoDB内部,不会暴露给用户。

4个系统表通过固定的硬编码进行构建。具体原理流程如下。

2、数据字典创建及加载原理流程



3、说明

1)innobase_start_or_create_for_mysql函数调用dict_create()函数进行数据字典的创建和加载工作。

2)dict_hdr_create完成系统表空间第7号页面dict header的初始化及创建SYS_TABLES两个索引、SYS_COLUMNS一个索引、SYS_INDEXES一个索引、SYS_FIELDS一个索引,其创建索引的函数是btr_create。

3)创建B+树索引后,通过dict_boot函数加载常驻内存的4个系统表。具体流程见流程图的②部分。

4)加载完成后,将这4个系统表挂在一个全局字典中:

dict0dict.h::

点击(此处)折叠或打开

  1. /* Dictionary system struct */
  2. struct dict_sys_t{
  3.     ib_mutex_t mutex; /*!< mutex protecting the data
  4.                     dictionary; protects also the
  5.                     disk-based dictionary system tables;
  6.                     this mutex serializes CREATE TABLE
  7.                     and DROP TABLE, as well as reading
  8.                     the dictionary data for a table from
  9.                     system tables */
  10.     row_id_t row_id; /*!< the next row id to assign;
  11.                     NOTE that at a checkpoint this
  12.                     must be written to the dict system
  13.                     header and flushed to a file; in
  14.                     recovery this must be derived from
  15.                     the log records */
  16.     hash_table_t* table_hash; /*!< hash table of the tables, based
  17.                     on name */
  18.     hash_table_t* table_id_hash; /*!< hash table of the tables, based
  19.                     on id */
  20.     ulint size; /*!< varying space in bytes occupied
  21.                     by the data dictionary table and
  22.                     index objects */
  23.     dict_table_t* sys_tables; /*!< SYS_TABLES table */
  24.     dict_table_t* sys_columns; /*!< SYS_COLUMNS table */
  25.     dict_table_t* sys_indexes; /*!< SYS_INDEXES table */
  26.     dict_table_t* sys_fields; /*!< SYS_FIELDS table */
  27.   
  28.     /*=============================*/
  29.     UT_LIST_BASE_NODE_T(dict_table_t)
  30.             table_LRU; /*!< List of tables that can be evicted
  31.                     from the cache */
  32.     UT_LIST_BASE_NODE_T(dict_table_t)
  33.             table_non_LRU; /*!< List of tables that can

结构体中sys_tables、sys_columns、sys_indexes、sys_fields四个结构存储上述对应的4个系统表。

结构体中HASH表及链表用来存储InnoDB中的所有表的缓存,包括系统表及用户表。table_hash哈希表按名字缓存,table_id_hash按表ID进行hash,LRU链表用来管理表对象缓存。

5)普通用户表加载流程见流程图的③、④部分。

当用户访问一个用户表时,首先需要从表对象缓存中查找这个表的SHARE对象,如果找到则直接从其实例化表对象链表中拿一个使用;如果没有找到,则需要重新打开这个表,需要找到这个表的字典信息。即③的流程。

具体加载一个表的字典是④流程,dict_load_table的工作。

a)首先需要找到SYS_TABLES表,也是先找缓存,缓存找不到再从系统表加载: dict_table_get_low

b)找到之后构建一个查询键值,从SYS_TABLES的name主键索引进行查询,如果诶呦找到或者该记录已经被删除则返回,否则解析找到的这条记录。然后根据这些信息创建表的内存对象table。

c)加载列操作与加载表的原理基本一样,对应系统表的SYS_COLUMNS,聚集索引为(TABLE_ID,POS),查找时,如果TABLE_ID相同,在POS从小到大排序,所以构造所有列的键值时,只需要通过TABLE_ID查询即可,按顺序取出所有列信息一一构造内存对象。

d)加载索引信息类似的流程


请使用浏览器的分享功能分享到微信等