MySQL系列(8)--InnoDB数据字典

什么是数据字典?

用来存储元数据信息的表。比如表的描述、字段、对象、对象之间的关系。InnoDB中大多以系统表的形式保存。主要包括sys_tables,sys_columns,sys_indexs,sys_fields
sys_table存储表的信息,包括表面,ID,表空间号
sys_columns存储表中列的信息,包括列名,列ID,列的序号,列的类型,长度等信息
sys_index存储表的索引信息,包括索引名,索引对应的表空间,表ID,索引ID,索引类型
sys_fields存储索引中定义的索引列

InnoDB数据字典启动过程

这里写图片描述

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::

/* 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 dictionary 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 be written to the dict system 
                header and flushed to a file; in 
                recovery this must be derived from 
                the log records */  
hash_table_t*   table_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 */  
dict_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)  
        table_LRU;  /*!< List of tables that can be evicted 
                from the cache */  
UT_LIST_BASE_NODE_T(dict_table_t)  
        table_non_LRU;  /*!< List of tables that can't be 
                evicted from the cache */  
};

结构体中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)加载索引信息类似的流程

猜你喜欢

转载自blog.csdn.net/duanxiaobin2010/article/details/80747347
今日推荐