一、存儲引擎
MySQL 資料庫使用不同的存儲引擎來支持不同的需求,如性能、事務支持、並發性等。目前,MySQL 支持的存儲引擎有 MyISAM、InnoDB、Memory、CSV、Archive 等。下面介紹 MyISAM 存儲引擎的一些數據結構。
二、MyISAM
1. 數據文件
MyISAM 存儲引擎的數據文件由數據和索引兩部分組成。數據文件的命名規則為表名加上擴展名 .MYD,而索引文件的命名規則為表名加上擴展名 .MYI。
/*******************************************************************************
MyISAM 數據文件格式
*******************************************************************************/
struct st_myisam_data_file_def
{
uchar key_buff[MAX_KEY_LENGTH + 8];
ulonglong records;
uint max_data_file_length;
uint data_file_length;
uint deleted_rows;
uint min_data_file_length;
uint auto_increment;
uint checksum;
uchar unused[3];
uchar concurrent;
uchar compressed;
};
2. 索引文件
MyISAM 的索引文件以 B+ 樹結構來保存索引,它將數據按鍵值大小排序。MySQL 採用的是定長記錄和變長記錄兩種索引方式。定長記錄的每一條記錄長度相等,變長記錄的每一條記錄長度都不相等。
/*******************************************************************************
MyISAM 索引文件格式
*******************************************************************************/
struct st_myisam_index_def {
uchar unused1[8];
ulonglong key_length;
uchar keytype;
uint 16 bit_key_parts;
uint key_parts;
uchar unused2[13];
ulonglong rec_per_key;
ulonglong 16 start_key_file;
ulonglong 16 end_key_file;
uchar unused3[4];
};
3. 數據緩衝區
MyISAM 存儲引擎通過緩存管理器(cache manager)來緩存數據和索引。數據緩衝區實現了哈希表來優化檢索速度。
/*******************************************************************************
MyISAM 數據緩衝區格式
*******************************************************************************/
struct st_myisam_key_cache {
KEY_CACHE hash[MAX_CACHE_BLOCKS];
uchar *data_memory;
uchar *key_memory;
size_t mem_root;
size_t pre_alloc_blocks;
ulonglong block_length;
uint block_size;
uint key_cache_id;
};
三、InnoDB
1. 數據文件
InnoDB 存儲引擎的數據文件由數據和索引兩部分組成。數據文件的命名規則為表名加上擴展名 .ibd。與 MyISAM 不同的是,InnoDB 將數據和索引都存儲在一個數據文件中。
/*******************************************************************************
InnoDB 數據文件格式
*******************************************************************************/
struct Fil_header {
ulint state;
ulint space_id;
ulint size;
ulint page_size;
ulint zip_size;
...
};
2. 數據頁
InnoDB 存儲數據的基本單位是頁(page),頁的大小默認為 16KB。InnoDB 存儲引擎使用了 B+ 樹搜索演算法來管理數據頁,在同一索引樹上只有一個小範圍鎖。
/*******************************************************************************
InnoDB 數據頁格式
*******************************************************************************/
struct page_header_t {
page_t:unsigned magic_n:16; /* Magic number */
page_t:unsigned ofs_n:16; /* Offset to page directory end */
page_t:unsigned next:32; /* Next page */
page_t:unsigned prev:32; /* Previous page */
page_t:unsigned btr_seg_leaf:32;
page_t:unsigned free_offset:16; /* Start of the free list in bytes */
page_t:unsigned garbage_offset:16; /* Start of garbage in bytes */
page_t:unsigned n_directions:16; /* Number of FSEG "directions" */
page_t:unsigned heap_top:32; /* Address of highest heap byte */
page_t:unsigned reserved_3:2;
page_t:unsigned heap_no:14; /* Current heap number */
page_t:unsigned trx_id:32; /* Rollback segment id */
page_t:unsigned trx_offset: 14; /* Rollback segment header offset */
page_t:unsigned file_page_offset: 18;
page_t:unsigned zip_size: 24;
page_t:unsigned space_id: 26;
page_t:unsigned reserved_4: 6;
page_t:unsigned page_zip: 1;
...
};
3. 事務處理機制
為了支持事務,InnoDB 存儲引擎引入了 MVCC(多版本並發控制)機制。MVCC 使多個事務可以同時讀取同一行記錄,同時允許事務並發地進行修改操作。
/*******************************************************************************
InnoDB 事務處理機制
*******************************************************************************/
struct trx_sys_t
{
trx_id_t max_trx_id;
trx_id_t purge_trx_id;
ulint max_trx_undo_size;
};
4. 共享表空間
InnoDB 存儲引擎可以使用單一的共享表空間(Table Space),將多個表的數據和索引存儲在一個文件中,這種方式使得表與表之間的數據互相獨立,可靠性更高。
/*******************************************************************************
InnoDB 共享表空間格式
*******************************************************************************/
struct tablespace_t
{
space_t *space;
ib_string file_path;
ib_string name;
ulint flags;
srv_minor_ver_t frm_ver;
};
四、總結
MySQL 資料庫的數據結構非常複雜,本文只粗略地介紹了 MyISAM 和 InnoDB 存儲引擎的一些數據結構。在使用 MySQL 資料庫時,需要根據具體的需求選擇合適的存儲引擎。同時,也需要了解每個存儲引擎的數據結構,以便更好地進行資料庫設計和性能優化。
原創文章,作者:GRQOP,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/371122.html
微信掃一掃
支付寶掃一掃