溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

InnoDB數(shù)據(jù)字典--字典表加載

發(fā)布時(shí)間:2020-06-02 21:01:26 來(lái)源:網(wǎng)絡(luò) 閱讀:5505 作者:yzs的專(zhuān)欄 欄目:MySQL數(shù)據(jù)庫(kù)

1、介紹

    在InnoDB啟動(dòng)時(shí),如果是新建數(shù)據(jù)庫(kù)則需初始化庫(kù),需要?jiǎng)?chuàng)建字典管理的相關(guān)信息。函數(shù)innobase_start_or_create_for_mysql調(diào)用dict_create完成此功能。即創(chuàng)建數(shù)據(jù)字典,因?yàn)镮nnoDB系統(tǒng)表的個(gè)數(shù)結(jié)構(gòu)固定,所以初始化庫(kù)的時(shí)候只需要?jiǎng)?chuàng)建這幾個(gè)表的B+樹(shù)即可并將B+樹(shù)的根頁(yè)號(hào)存放到固定位置。對(duì)于B+樹(shù),只要找到根頁(yè)面,就可以從根頁(yè)面開(kāi)始檢索數(shù)據(jù)。相關(guān)系統(tǒng)表(即上一節(jié)講到的4個(gè)系統(tǒng)表)在InnoDB內(nèi)部,不會(huì)暴露給用戶。

    4個(gè)系統(tǒng)表通過(guò)固定的硬編碼進(jìn)行構(gòu)建。具體原理流程如下。

2、數(shù)據(jù)字典創(chuàng)建及加載原理流程
InnoDB數(shù)據(jù)字典--字典表加載
3、說(shuō)明

1)innobase_start_or_create_for_mysql函數(shù)調(diào)用dict_create()函數(shù)進(jìn)行數(shù)據(jù)字典的創(chuàng)建和加載工作。

2)dict_hdr_create完成系統(tǒng)表空間第7號(hào)頁(yè)面dict header的初始化及創(chuàng)建SYS_TABLES兩個(gè)索引、SYS_COLUMNS一個(gè)索引、SYS_INDEXES一個(gè)索引、SYS_FIELDS一個(gè)索引,其創(chuàng)建索引的函數(shù)是btr_create。

3)創(chuàng)建B+樹(shù)索引后,通過(guò)dict_boot函數(shù)加載常駐內(nèi)存的4個(gè)系統(tǒng)表。具體流程見(jiàn)流程圖的②部分。

4)加載完成后,將這4個(gè)系統(tǒng)表掛在一個(gè)全局字典中:

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 */  
}; 

結(jié)構(gòu)體中sys_tables、sys_columns、sys_indexes、sys_fields四個(gè)結(jié)構(gòu)存儲(chǔ)上述對(duì)應(yīng)的4個(gè)系統(tǒng)表。

結(jié)構(gòu)體中HASH表及鏈表用來(lái)存儲(chǔ)InnoDB中的所有表的緩存,包括系統(tǒng)表及用戶表。table_hash哈希表按名字緩存,table_id_hash按表ID進(jìn)行hash,LRU鏈表用來(lái)管理表對(duì)象緩存。

5)普通用戶表加載流程見(jiàn)流程圖的③、④部分。

當(dāng)用戶訪問(wèn)一個(gè)用戶表時(shí),首先需要從表對(duì)象緩存中查找這個(gè)表的SHARE對(duì)象,如果找到則直接從其實(shí)例化表對(duì)象鏈表中拿一個(gè)使用;如果沒(méi)有找到,則需要重新打開(kāi)這個(gè)表,需要找到這個(gè)表的字典信息。即③的流程。

具體加載一個(gè)表的字典是④流程,dict_load_table的工作。

a)首先需要找到SYS_TABLES表,也是先找緩存,緩存找不到再?gòu)南到y(tǒng)表加載: dict_table_get_low

b)找到之后構(gòu)建一個(gè)查詢鍵值,從SYS_TABLES的name主鍵索引進(jìn)行查詢,如果誒呦找到或者該記錄已經(jīng)被刪除則返回,否則解析找到的這條記錄。然后根據(jù)這些信息創(chuàng)建表的內(nèi)存對(duì)象table。

c)加載列操作與加載表的原理基本一樣,對(duì)應(yīng)系統(tǒng)表的SYS_COLUMNS,聚集索引為(TABLE_ID,POS),查找時(shí),如果TABLE_ID相同,在POS從小到大排序,所以構(gòu)造所有列的鍵值時(shí),只需要通過(guò)TABLE_ID查詢即可,按順序取出所有列信息一一構(gòu)造內(nèi)存對(duì)象。

d)加載索引信息類(lèi)似的流程

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI