存储引擎
# 存储引擎
不同的存储引擎存储数据的方式是不一样的。是 mysql 中特有的术语。
实际上存储引擎就是一张表存储数据组织数据的方式。
# 给表添加指定存储引擎
mysql> show create table student;
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) DEFAULT NULL,
`name` varchar(60) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`chinese` double DEFAULT NULL,
`gender` char(3) DEFAULT NULL,
`birthday` datetime DEFAULT NULL,
`math` int(11) DEFAULT NULL,
`english` int(11) DEFAULT NULL,
`address` varchar(20) DEFAULT NULL,
`grade` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mysql 默认的存储引擎是 innoDB,默认的字符集是 utf8。
创建表时指定存储引擎:
mysql> create table t_test(id int,name varchar(255))engine=innoDB default charset=gbk;
Query OK, 0 rows affected (0.01 sec)
mysql> show create table t_test;
+--------+-----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+-----------------------------------------------------------------------------------------------------------------------------+
| t_test | CREATE TABLE `t_test` (
`id` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+--------+-----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# mysql 支持的存储引擎有哪些
mysql> show engines \g
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
一张表中主键字段或者加有 unique 约束的字段上都会自动创建索引。
MyISAM storage engine 是 mysql 常用的引擎。
- 使用三个文件表示每个表。
- 格式文件:mytable.frm
- 数据文件: mytable.MYD
- 索引文件:mytable.MYI 相当于一本书的目录。提高查询效率。
- 灵活的 AUTO_INCREMENT 字段处理。
- 可被转换为压缩、只读表来节省空间。优势!
InnoDB storage engine 是 mysql 默认的引擎。
- 非常安全
- 在目录中以 frm 格式的文件表示
- InnoDB 表空间 tablespace 被用于存储表的内容
- 提供一组用来记录事务性活动的日志文件
- 用 COMMIT 提交、SAVEPOINT 及 ROLLBACK 支持事务处理
- 提供全 ACID 兼容
- 在 MYSQL 服务器崩溃后提供自动恢复
- 在多版本 MVCC 和行级锁定
- 支持外键及引用的完整性,包括级联删除和更新
Memory storage engine 数据存储在内存中,且行的长度固定。查询效率最高但是不安全。关机后数据消失。
- 在数据库目录内,每个表均以 .frm 格式的文件表示
- 在数据及索引被存储在内存中
- 表级锁机制
- 不能包含 TEXT 或 BLOB 字段
编辑 (opens new window)
上次更新: 2025/02/10, 20:20:37