1、什么是存储引擎?

存储引擎就是指表的类型。数据库的存储类型决定了表在计算机中的存储方式,用户可以根据不同的存储方式、是否进行事务处理等来选择合适的存储引擎。

2、如何查看MySQL中的存储引擎?

  • 查看MySQL支持的存储引擎:SHOW ENGINES
  • 查看显示支持的存储引擎信息:SHOW VARIABLES LIKE 'have%'
  • 查看默认的存储引擎:SHOW VARIABLES LIKE 'storage_engine'
1 > 查看MySQL支持的存储引擎:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql> SHOW ENGINES;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

如果在命令行查看会很臃肿难看,我们可以使用 \G 来用网格模式输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
mysql> SHOW ENGINES\G;
*************************** 1. row ***************************
Engine: InnoDB
Support: DEFAULT
Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
XA: YES
Savepoints: YES
*************************** 2. row ***************************
Engine: MRG_MYISAM
Support: YES
Comment: Collection of identical MyISAM tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 3. row ***************************
Engine: MEMORY
Support: YES
Comment: Hash based, stored in memory, useful for temporary 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: MyISAM
Support: YES
Comment: MyISAM storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 6. row ***************************
Engine: CSV
Support: YES
Comment: CSV storage engine
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: PERFORMANCE_SCHEMA
Support: YES
Comment: Performance Schema
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)

  1、Engine:存储引擎的名称
  2、Support:MySQL是否支持这种引擎
  3、Comment:对此引擎的注释
  4、Transactions:是否支持事务处理
  5、XA:是否是分布式交易处理
  6、Savepoints:是否支持保存点

2 > 查看显示支持的存储引擎信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mysql> SHOW VARIABLES LIKE 'have%';
+------------------------+----------+
| Variable_name | Value |
+------------------------+----------+
| have_compress | YES |
| have_crypt | YES |
| have_dynamic_loading | YES |
| have_geometry | YES |
| have_openssl | DISABLED |
| have_profiling | YES |
| have_query_cache | YES |
| have_rtree_keys | YES |
| have_ssl | DISABLED |
| have_statement_timeout | YES |
| have_symlink | YES |
+------------------------+----------+
11 rows in set (0.01 sec)
3 > 查看默认的存储引擎:
1
2
3
4
5
6
7
mysql> SHOW VARIABLES LIKE 'storage_engine';
+------------------------+----------+
| Variable_name | Value |
+------------------------+----------+
| storage_engine | InnoDB |
+------------------------+----------+
1 rows in set (0.00 sec)

3、MySQL常用存储引擎及特点

  • InnoDB,用于事务处理应用程序,具有众多特性,包括ACID事务支持
  • MyISAM,在Web、数据仓储和其他应用环境下最常使用的存储引擎之一
  • MEMORY,将所有数据保存在RAM中,可提供极快的访问。

4、如何选择?

  • 是否支持事务
  • 是否有存储限制
  • 是否是空间使用还是内存使用
  • 对插入数据的速度
  • 是否支持外键等