【入门篇】2.6 MySQL存储引擎简介

MySQL存储引擎简介
目录
1 存储引擎的概念
2 MySQL存储引擎分类及适用场景
4 怎样查看和设置存储引擎
4.1查看MySQL支持的存储引擎
4.2 查看表的存储引擎
4.3 设置存储引擎
4 存储引擎的比较
4.1 InnoDB vs MyISAM
4.2 InnoDB vs Memory
1 存储引擎的概念
- 存储引擎是数据库管理系统用来存储、处理和保护数据的核心组件。
- MySQL的灵活性之一在于,它支持多种存储引擎,每种存储引擎都有其独特的特点。
2 MySQL存储引擎分类及适用场景
- InnoDB: The default storage engine in MySQL 5.7.
InnoDB
is a transaction-safe (ACID compliant) storage engine for MySQL that has commit, rollback, and crash-recovery capabilities to protect user data.InnoDB
row-level locking (without escalation to coarser granularity locks) and Oracle-style consistent nonlocking reads increase multi-user concurrency and performance.InnoDB
stores user data in clustered indexes to reduce I/O for common queries based on primary keys. To maintain data integrity,InnoDB
also supportsFOREIGN KEY
referential-integrity constraints. For more information aboutInnoDB
, see Chapter 14, The InnoDB Storage Engine. - MyISAM: These tables have a small footprint. Table-level locking limits the performance in read/write workloads, so it is often used in read-only or read-mostly workloads in Web and data warehousing configurations.
- Memory: Stores all data in RAM, for fast access in environments that require quick lookups of non-critical data. This engine was formerly known as the
HEAP
engine. Its use cases are decreasing;InnoDB
with its buffer pool memory area provides a general-purpose and durable way to keep most or all data in memory, andNDBCLUSTER
provides fast key-value lookups for huge distributed data sets. - CSV: Its tables are really text files with comma-separated values. CSV tables let you import or dump data in CSV format, to exchange data with scripts and applications that read and write that same format. Because CSV tables are not indexed, you typically keep the data in
InnoDB
tables during normal operation, and only use CSV tables during the import or export stage. - Archive: These compact, unindexed tables are intended for storing and retrieving large amounts of seldom-referenced historical, archived, or security audit information.
- Blackhole: The Blackhole storage engine accepts but does not store data, similar to the Unix
/dev/null
device. Queries always return an empty set. These tables can be used in replication configurations where DML statements are sent to replica servers, but the source server does not keep its own copy of the data. - NDB (also known as NDBCLUSTER): This clustered database engine is particularly suited for applications that require the highest possible degree of uptime and availability.
- Merge(also known as the
MRG_MyISAM
): Enables a MySQL DBA or developer to logically group a series of identicalMyISAM
tables and reference them as one object. Good for VLDB environments such as data warehousing. - Federated: Offers the ability to link separate MySQL servers to create one logical database from many physical servers. Very good for distributed or data mart environments.
- Example: This engine serves as an example in the MySQL source code that illustrates how to begin writing new storage engines. It is primarily of interest to developers. The storage engine is a “stub” that does nothing. You can create tables with this engine, but no data can be stored in them or retrieved from them.
You are not restricted to using the same storage engine for an entire server or schema. You can specify the storage engine for any table. For example, an application might use mostly InnoDB
tables, with one CSV
table for exporting data to a spreadsheet and a few MEMORY
tables for temporary workspaces.
4 怎样查看和设置存储引擎
4.1查看MySQL支持的存储引擎
查看自己的MySQL版本支持哪些引擎:
mysql> show engines;
4.2 查看表的存储引擎
show create table 表名;
结果:
CREATE TABLE `employees` ( `id` int(11) NOT NULL, `name` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL, `department` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL, `salary` decimal(10,2) DEFAULT NULL, `hire_date` date DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
4.3 设置存储引擎
在创建表的时候在create table 时指定 ENGINE=存储引擎名字;
create table test_engine (a int) ENGINE = MyISAM; show create table test_engine;
4 存储引擎的比较
4.1 InnoDB vs MyISAM
- 事务支持: InnoDB提供事务支持,而MyISAM则不支持。如果你的系统需要执行复杂的事务操作,那么你应该选择InnoDB。
- 锁定级别: MyISAM只支持表级锁,而InnoDB则支持行级锁和表级锁。这意味着在并发环境下,InnoDB通常能提供更高的吞吐量。
- 全文搜索: MyISAM支持全文搜索,而InnoDB在MySQL 5.6之前的版本中不支持。但是从MySQL 5.6开始,InnoDB也开始支持全文搜索。
- 数据完整性: InnoDB支持外键和完整性约束,这有助于确保数据的完整性和准确性。而MyISAM则不支持。
- 崩溃恢复: InnoDB有更好的崩溃恢复能力。如果MySQL服务器意外停止,InnoDB存储引擎可以利用日志文件自动恢复到一致的状态,而MyISAM可能需要手动修复。
4.2 InnoDB vs Memory
- 数据持久性: InnoDB存储的数据是持久化的,服务器重启后数据不会丢失。而Memory存储引擎的数据存储在内存中,服务器重启后数据会丢失。
- 数据容量: 由于Memory存储引擎的数据存储在内存中,因此其数据容量受到内存大小的限制,通常不能存储大量数据。而InnoDB存储的数据存储在磁盘中,可以存储更多的数据。
- 查询速度: Memory存储引擎的数据存储在内存中,查询速度通常比InnoDB快。但是如果数据量过大,超过了内存容量,Memory的性能会大大下降。