MySQL 的所有存儲引擎概述
MySQL 有一個存儲引擎的概念,針對不同的應用場景,可以選擇不同的存儲引擎,這也是 MySQL 區(qū)別于其他數(shù)據(jù)庫的重要特征。本章將介紹存儲引擎的基本概念、分類,以及如何選擇合適的存儲引擎。
1. MySQL 存儲引擎概述
MySQL 的存儲引擎是插件式的,用戶可以根據(jù)實際的應用場景,選擇最佳的存儲引擎。MySQL默認支持多種存儲引擎,以適應不同的應用需求。
MySQL 5.7 支持的存儲引擎有:InnoDB、MyISAM、MEMORY、CSV、MERGE、FEDERATED 等。從 5.5.5 版本開始,InnoDB 成為 MySQL 的默認存儲引擎,也是當前最常用的存儲引擎,5.5.5 版本之前,默認引擎為 MyISAM。創(chuàng)建新表時,如果不指定存儲引擎,MySQL 會使用默認存儲引擎。
使用以下命令,查看數(shù)據(jù)庫當前的默認引擎:
mysql> show variables like 'default_storage_engine';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+
1 row in set (0.00 sec)
使用以下命令,查看數(shù)據(jù)庫當前所支持的存儲引擎:
mysql> show engines\G
*************************** 1. row ***************************
Engine: MEMORY
Support: YES
Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 2. row ***************************
Engine: CSV
Support: YES
Comment: CSV storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 3. row ***************************
Engine: MRG_MYISAM
Support: YES
Comment: Collection of identical MyISAM tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 4. row ***************************
Engine: BLACKHOLE
Support: YES
Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
XA: NO
Savepoints: NO
*************************** 5. row ***************************
Engine: InnoDB
Support: DEFAULT
Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
XA: YES
Savepoints: YES
*************************** 6. row ***************************
Engine: PERFORMANCE_SCHEMA
Support: YES
Comment: Performance Schema
Transactions: NO
XA: NO
Savepoints: NO
*************************** 7. row ***************************
Engine: ARCHIVE
Support: YES
Comment: Archive storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 8. row ***************************
Engine: MyISAM
Support: YES
Comment: MyISAM storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 9. row ***************************
Engine: FEDERATED
Support: NO
Comment: Federated MySQL storage engine
Transactions: NULL
XA: NULL
Savepoints: NULL
9 rows in set (0.00 sec)
每一行的含義大致如下:
- Engine:存儲引擎名稱;
- Support:不同值的含義為:
- DEFAULT:表示支持并啟用,為默認引擎;
- YES:表示支持并啟用;
- NO:表示不支持;
- DISABLED:表示支持,但是被數(shù)據(jù)庫禁用。
- Comment:存儲引擎注釋;
- Transactions:是否支持事務;
- XA:是否支持XA分布式事務;
- Savepoints:是否支持保存點。
創(chuàng)建表時,ENGINE 關鍵字表示表的存儲引擎。如下例子中,表 a 的存儲引擎為 InnoDB,表 b 的存儲引擎為 MyISAM。
mysql> create table a (id int) ENGINE = InnoDB;
Query OK, 0 rows affected (0.01 sec)
mysql> create table b (id int) ENGINE = MyISAM;
Query OK, 0 rows affected (0.01 sec)
也可以使用 show table status
命令查看表的相關信息。
mysql> show table status like 'a'\G
*************************** 1. row ***************************
Name: a
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 1
Avg_row_length: 16384
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2020-04-21 02:29:06
Update_time: 2020-04-29 00:24:17
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
每一行的含義大致如下:
- Name:表名;
- Engine:表的存儲引擎類型;
- Version:版本號;
- Row_format:行的格式
- Rows:表中的行數(shù);
- Avg_row_length:平均每行包含的字節(jié)數(shù);
- Data_length:表數(shù)據(jù)的大小(單位字節(jié));
- Max_data_length:表數(shù)據(jù)的最大容量;
- Index_length:索引的大小(單位字節(jié));
- Data_free:已分配但目前沒有使用的空間,可以理解為碎片空間(單位字節(jié));
- Auto_increment:下一個 Auto_increment 值;
- Create_time:表的創(chuàng)建時間;
- Update_time:表數(shù)據(jù)的最后修改時間;
- Check_time:使用check table命令,最后一次檢查表的時間;
- Collation:表的默認字符集和字符列排序規(guī)則;
- Checksum:如果啟用,保存的是整個表的實時校驗和;
- Create_options:創(chuàng)建表時指定的其他選項;
- Comment:表的一些額外信息。
2. 小結
本小節(jié)介紹了 MySQL 存儲引擎的基本概念,以及一些相關命令,如查看當前默認引擎、查看數(shù)據(jù)庫當前所支持的存儲引擎、查看表的相關信息等。