Simple understanding of Mysql storage engine InnoDB

Digression

The storage engine is the core of the database, and the storage engine is equivalent to the engine of data storage to drive data to be stored at the disk level. For MySQL, the storage engine runs as a plug-in. Although MySQL supports a wide range of storage engines, the most commonly used one is InnoDB. This article will mainly introduce InnoDB storage engine related knowledge. .

1. Introduction to InnoDB

After MySQL 5.5, the default storage engine is InnoDB. InnoDB is a general-purpose storage engine that combines high reliability and high performance. The storage engine is also a component of MySQL, which is a kind of software.

Simple understanding of mysql architecture:
Insert picture description here

# 查看MySQL支持的存储引擎
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       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+

# 查看默认存储引擎
mysql> show variables like 'default_storage_engine';
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+

#查看表latest的存储引擎
show CREATE table latest;
CREATE TABLE `latest` (
  `GrpContNo` varchar(20) NOT NULL,
  `ContNo` varchar(30) NOT NULL DEFAULT '',
  `ProposalContNo` varchar(20) NOT NULL,
  `AgentGroup` varchar(12) DEFAULT NULL,
  `AgentCode1` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`ContNo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

#修改表latest的存储引擎
ALTER TABLE LATEST ENGINE=InnoDB;

2. Advantages of InnoDB

1)支持事务
InnoDB 最重要的一点就是支持事务,可以说这是 InnoDB 成为 MySQL 中最流行的存储引擎的一个非常重要的原因。此外 InnoDB 还实现了 4 种隔离级别(READ UNCOMMITTED,READ COMMITTED,REPEATABLE READ 和 SERIALIZABLE),使得对事务的支持更加灵活。

2)灾难恢复性好
InnoDB 通过 commit、rollback、crash-recovery 来保障数据的安全。

具体来说,crash-recovery 就是指如果服务器因为硬件或软件的问题而崩溃,不管当时数据是怎样的状态,在重启 MySQL 后,InnoDB 都会自动恢复到发生崩溃之前的状态。

3)使用行级锁
InnoDB 改变了 MyISAM 的锁机制,实现了行锁。虽然 InnoDB 的行锁机制是通过索引来完成的,但毕竟在数据库中大部分的 SQL 语句都要使用索引来检索数据。行锁定机制也为 InnoDB 在承受高并发压力的环境下增强了不小的竞争力。

4)实现了缓冲处理
InnoDB 提供了专门的缓冲池,实现了缓冲管理,不仅能缓冲索引也能缓冲数据,常用的数据可以直接从内存中处理,比从磁盘获取数据处理速度要快。在专用数据库服务器上,通常会将最多80%的物理 memory 分配给缓冲池。

5)支持外键
InnoDB 支持外键约束,检查外键、插入、更新和删除,以确保数据的完整性。存储表中的数据时,每张表的存储都按主键顺序存放,如果没有显式在表定义时指定主键,InnoDB 会为每一行生成一个6字节的 ROWID ,并以此作为主键。

Three, other commonly used storage engines

MyISAM storage engine

在 5.1 版本之前,MyISAM 是 MySQL 的默认存储引擎,MyISAM 并发性比较差,使用的场景比较少,主要特点是:

1)不支持事务操作,ACID 的特性也就不存在了,这一设计是为了性能和效率考虑的。

2)不支持外键操作,如果强行增加外键,MySQL 不会报错,只不过外键不起作用。

3)MyISAM 默认的锁粒度是表级锁,所以并发性能比较差,加锁比较快,锁冲突比较少,不太容易发生死锁的情况。

4)MyISAM 会在磁盘上存储三个文件,文件名和表名相同,扩展名分别是 .frm(存储表定义)、.MYD(MYData,存储数据)MYI(MyIndex,存储索引)。这里需要特别注意的是 MyISAM 只缓存索引文件,并不缓存数据文件。

5)MyISAM 支持的索引类型有 全局索引(Full-Text)、B-Tree 索引、R-Tree 索引

6)Full-Text 索引:它的出现是为了解决针对文本的模糊查询效率较低的问题。

7)B-Tree 索引:所有的索引节点都按照平衡树的数据结构来存储,所有的索引数据节点都在叶节点

8)R-Tree索引:它的存储方式和 B-Tree 索引有一些区别,主要设计用于存储空间和多维数据的字段做索引,目前的 MySQL 版本仅支持 geometry 类型的字段作索引,相对于 BTREE,RTREE 的优势在于范围查找。

9)数据库所在主机如果宕机,MyISAM 的数据文件容易损坏,而且难以恢复。

10)增删改查性能方面:SELECT 性能较高,适用于查询较多的情况。

MEMORY storage engine

MEMORY 存储引擎使用存在内存中的内容来创建表。每个 MEMORY 表实际只对应一个磁盘文件,格式是 .frm。MEMORY 类型的表访问速度很快,因为其数据是存放在内存中。默认使用 HASH 索引。

Three, summary

This article briefly introduces several storage engines commonly used in Mysql, especially the most commonly used and recommended InnoDB engine. If you want to use other storage engines, you must choose carefully in combination with the scene.

Oh, not bad! ------Welcome to point out the mistakes and add better methods

Guess you like

Origin blog.csdn.net/Tah_001/article/details/108530047