MySQL 有一个存储引擎的概念,针对不同的应用场景,可以选择不同的存储引擎,这也是 MySQL 区别于其他数据库的重要特征。本章将介绍存储引擎的基本概念、分类,以及如何选择合适的存储引擎。
MySQL 的存储引擎是插件式的,用户可以根据实际的应用场景,选择最佳的存储引擎。MySQL默认支持多种存储引擎,以适应不同的应用需求。
MySQL 5.7 支持的存储引擎有:InnoDB、MyISAM、MEMORY、CSV、MERGE、FEDERATED 等。从 5.5.5 版本开始,InnoDB 成为 MySQL 的默认存储引擎,也是当前最常用的存储引擎,5.5.5 版本之前,默认引擎为 MyISAM。创建新表时,如果不指定存储引擎,MySQL 会使用默认存储引擎。
使用以下命令,查看数据库当前的默认引擎:
mysql> show variables like 'default_storage_engine';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+
1 row in set (0.00 sec)
使用以下命令,查看数据库当前所支持的存储引擎:
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 关键字表示表的存储引擎。如下例子中,表 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)
每一行的含义大致如下:
本小节介绍了 MySQL 存储引擎的基本概念,以及一些相关命令,如查看当前默认引擎、查看数据库当前所支持的存储引擎、查看表的相关信息等。
0/1000