1. 程式人生 > MYSQL進階教學 ><p>MySQL 的所有儲存引擎概述</p>

<p>MySQL 的所有儲存引擎概述</p>

MySQL 有一個儲存引擎的概念,針對不同的應用場景,可以選擇不同的儲存引擎,這也是 MySQL 區別於其他資料庫的重要特徵。本章將介紹儲存引擎的基本概念、分類,以及如何選擇合適的儲存引擎。

1. 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:儲存引擎名稱;
  • Support:不同值的含義為:
    • DEFAULT:表示支援並啟用,為預設引擎;
    • YES:表示支援並啟用;
    • NO:表示不支援;
    • DISABLED:表示支援,但是被資料庫禁用。
  • Comment:儲存引擎註釋;
  • Transactions:是否支援事務;
  • XA:是否支援XA分散式事務;
  • Savepoints:是否支援儲存點。

建立表時,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:表中的行數;
  • Avg_row_length:平均每行包含的位元組數;
  • Data_length:表資料的大小(單位位元組);
  • Max_data_length:表資料的最大容量;
  • Index_length:索引的大小(單位位元組);
  • Data_free:已分配但目前沒有使用的空間,可以理解為碎片空間(單位位元組);
  • Auto_increment:下一個 Auto_increment 值;
  • Create_time:表的建立時間;
  • Update_time:表資料的最後修改時間;
  • Check_time:使用check table命令,最後一次檢查表的時間;
  • Collation:表的預設字符集和字元列排序規則;
  • Checksum:如果啟用,儲存的是整個表的實時校驗和;
  • Create_options:建立表時指定的其他選項;
  • Comment:表的一些額外資訊。

2. 小結

本小節介紹了 MySQL 儲存引擎的基本概念,以及一些相關命令,如檢視當前預設引擎、檢視資料庫當前所支援的儲存引擎、查看錶的相關資訊等。