---
title: "【MySQL】两种存储引擎 —— MyISAM vs InnoDB"
author: "草料场"
date: "2019-03-06T14:32:49Z"
tags: []
categories: ["数据库"]
slug: "mysql-myisam-vs-innodb"
draft: false
lastmod: "2019-03-14T09:06:33Z"
id: 2438470
url: "https://pre.iteye.com/blog/2438470"
---

InnoDB 和 MyISAM 是MySQL中最常见的两种存储引擎。

通常：

- **读操作密集的表，选MyISAM**
- **写操作密集的表，选InnoDB**

 

# 二选一需要考虑哪些特性

以下特性是 InnoDB 和 MyISAM 二选一时通常需要考虑的特性。

## [MyISAM](https://dev.mysql.com/doc/refman/8.0/en/myisam-storage-engine.html)

MyISAM 基于 [ISAM](https://en.wikipedia.org/wiki/ISAM)（Indexed Sequential Access Method）改良而来。**性能好**，但是：

- **只有表锁，没有行锁**。
  
  读数据时，对所有读到的表加锁；
  
  写数据时，对表加排它锁。
- **不支持事务**
- **不支持崩溃后安全恢复**
- **不支持外键**

 

## [InnoDB](https://dev.mysql.com/doc/refman/8.0/en/innodb-introduction.html)

InnoDB 是一种 通用型 的存储引擎，它在高可用和高性能之间做了平衡。其主要特性有：

- **支持行锁**
  
  InnoDB 的行锁机制使用了[MVCC](https://dev.mysql.com/doc/refman/5.7/en/innodb-multi-versioning.html)（Multiversion Concurrency Control）（事务内部保留了一份原数据的[快照](https://www.valinv.com/dev/mysql-what-is-mysql-mvcc)）
- **支持事务**
- **支持崩溃后安全恢复**
- **支持外键**

此外，InnoDB 会利用主键优化查询效率。通过主键的[**聚集索引**](https://dev.mysql.com/doc/refman/8.0/en/innodb-index-types.html)（clustered index），可以减少按主键查询时的IO次数。

 

**绝大多数情况下，可以选择InnoDB作为存储引擎**。外键、事务、崩溃后安全恢复、行锁，这些特性是很常见的需求。

 

# 更多特性对比

更多：[InnoDB](https://dev.mysql.com/doc/refman/8.0/en/innodb-introduction.html)、[MyISAM](https://dev.mysql.com/doc/refman/8.0/en/myisam-storage-engine.html)

 

**特性** **InnoDB** **MyISAM** 聚集索引 支持 **不支持** 全文索引（FULLTEXT INDEX） 支持（[从5.6开始](https://dev.mysql.com/doc/refman/5.6/en/innodb-fulltext-index.html)） 支持 地理数据索引（SPATIAL INDEX） 支持（[从5.7开始](https://dev.mysql.com/doc/refman/5.7/en/creating-spatial-indexes.html)） 支持