1. Introduction
在InnoDB启动时,如果是新建数据库则需初始化库,需要创建字典管理的相关信息。函数innobase_start_or_create_for_mysql调用dict_create完成此功能。即创建数据字典,因为InnoDB系统表的个数结构固定,所以初始化库的时候只需要创建这几个表的B+树即可并将B+树的根页号存放到固定位置。对于B+树,只要找到根页面,就可以从根页面开始检索数据。相关系统表(即上一节讲到的4个系统表)在InnoDB内部,不会暴露给用户。 4个系统表通过固定的硬编码进行构建。具体原理流程如下。
2. Data dictionary creation and loading principle flow
3. Description
1) The Innobase_start_or_create_for_mysql function calls the Dict_create () function to create and load the data dictionary.
2) dict_hdr_create complete the System table Space page 7th Dict header initialization and creation Sys_tables two indexes, sys_columns an index, sys_indexes an index, sys_fields an index, The function whose index is created is btr_create.
3) After creating the B + Tree index, 4 system tables of resident memory are loaded via the Dict_boot function. The specific process is described in the ② section of the flowchart.
4) When the load is complete, the 4 system tables are hung in a global dictionary:
Dict0dict.h::
/* Dictionary system struct */struct dict_sys_t{ib_mutex_t mutex; /*!< mutex protecting the data dictionary; Protects also the disk-based dictionary system tables; This mutex serializes CREATE table and DROP table, as well as reading the Dictiona RY data for a table from system tables */row_id_t row_id; /*!< the next row ID to assign; NOTE that at a checkpoint this must is written to the Dict system header and flush Ed to a file; In recovery this must is derived from the log records */hash_table_t* Tabl E_hash; /*!< hash Table of the tables, based on name */hash_table_t* Table_id_hash; /*!< hash Table of the tables, based on ID */ulint size; /*!< varying Space in bytes occupied by the data dictionary table and Index objects */di ct_table_t* Sys_tables; /*!< sys_tables Table */dict_table_t* sys_columns; /*!< sys_columns Table */dict_table_t* sys_indexes; /*!< sys_indexes Table */dict_table_t* sys_fields; /*!< sys_fields Table *//*=============================*/ut_list_base_node_t (dict_table_t) tab LE_LRU; /*!< List of tables that can is evicted from the cache */ut_list_base_node_t (dict_table_t) TABLE_NON_LRU; /*!< List of tables that can ' t is evicted from the cache */};
The Sys_tables, Sys_columns, sys_indexes, sys_fields four structures in the structure store the corresponding 4 system tables.
Hash tables and linked lists in structs are used to store caches of all tables in InnoDB, including system tables and user tables. Table_hash hash table is cached by name, Table_id_hash is HASH,LRU by table ID to manage Table object cache.
5) Ordinary user table loading process see flowchart ③, ④ part.
When a user accesses a user table, it first needs to look up the table's share object from the Table object cache, and if found, it is used directly from its list of instantiated table objects, and if it is not found, it needs to be re-opened to find the table's dictionary information. That is, the ③ process.
The dictionary that loads a table specifically is the ④ process, dict_load_table work.
A) First you need to find the Sys_tables table, but also first look for the cache, the cache can not find and then load from the system table: Dict_table_get_low
b) Locate and then build a query key value, query from Sys_tables's name primary key index, or return if it is found or the record has been deleted, otherwise parse the found record. Then, based on this information, the table's memory object is created.
c) The Load column operation is basically the same as loading the table, corresponding to the sys_columns of the system table, the clustered index (TABLE_ID,POS), when looking, if the table_id is the same, in the POS from small to large sort, so when constructing all the columns of the key value, only need to pass Table_ ID query, and sequentially remove all column information one by one constructs the memory object.
D) A similar process for loading index information
InnoDB Data Dictionary--Dictionary table loading