Comparative analysis of MySQL storage engine InnoDB and MyISAM

InnoDB storage engine

InnoDB is a transactional storage engine designed to provide high-performance services when dealing with large amounts of data . It builds a buffer pool in memory at runtime for buffering data and indexes.

 

InnoDB Advantages

 

1. Support transaction processing and ACID transaction features ;

2. Four isolation levels of the SQL standard are implemented ;

3. Support row-level locks and foreign key constraints;

The row lock of InnoDB is only valid in the primary key of WHERE, and the WHERE of non-primary key will lock the whole table.

4. The transaction log can be used for data recovery .

 

InnoDB Disadvantages

 

1. The index of FULLTEXT type is not supported because it does not save the number of rows in the table. When using COUNT statistics, the whole table will be scanned.

 

MYSQL index has four kinds of PRIMARY, INDEX, UNIQUE, FULLTEXT, among them PRIMARY, INDEX, UNIQUE is one kind, FULLTEXT is one kind.

FULLTEXT is an index type.

 

String type: support TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT

 

InnoDB applicable scenarios

 

1. Operations that require transactions;

2. Row-level locks are required to update data;

3. Large amount of data read and write;

4. Large-scale Internet applications.

 

MyISAM storage engine

MyISAM is MySQL's default engine, and it's designed for fast reads .

 

MyISAM Advantages

 

1. High performance reading;

2. Because it saves the number of rows in the table, it will not scan the entire table when using COUNT statistics ;

3. Support full-text index of FULLTEXT type

 

MyISAM Disadvantages

 

1. Does not support database transactions ;

2. Only table-level locks are supported, row-level locks and foreign keys are not supported ;

3. INSERT and UPDATE operations need to lock the entire table;

4. Failure recovery is not supported ;

 

MyISAM Applicable Scenarios

 

1. Operations that do not require transactions;

2. Few inserts and updates, and frequent reads;

3. Frequent statistical calculations.

 

 

=======================================================

What is the difference between MyISAM and InnoDB?

 

1. Storage structure

 

MyISAM: Each MyISAM is stored as three files on disk. The name of the first file begins with the name of the table, and the extension indicates the file type. .frm files store table definitions. Data files have the extension .MYD (MYData). Index files have the extension .MYI (MYIndex).

InnoDB: All tables are stored in the same data file (may be multiple files, or independent tablespace files). The size of the InnoDB table is only limited by the size of the operating system file, generally 2GB.

 

2. Storage space

 

MyISAM: It can be compressed, and the storage space is small . Three different storage formats are supported: static table (default, but note that there can be no spaces at the end of the data, it will be removed), dynamic table, compressed table.

InnoDB: requires more memory and storage, it builds its dedicated buffer pool in main memory for caching data and indexes.

 

3. Portability, Backup and Recovery

 

MyISAM: Data is stored in the form of files, so it is very convenient in cross-platform data transfer. Operations can be performed individually on a table during backup and restore.

InnoDB: Free solutions can be to copy data files, backup binlog, or use mysqldump, which is relatively painful when the amount of data reaches tens of gigabytes.

 

4. Transaction support

 

MyISAM: The emphasis is on performance, each query is atomic, and its execution is several times faster than the InnoDB type, but it does not provide transaction support.

InnoDB:提供事务支持事务,外部键等高级数据库功能。 具有事务(commit)、回滚(rollback)和崩溃修复能力(crash recovery capabilities)的事务安全(transaction-safe (ACID compliant))型表。

 

5、 AUTO_INCREMENT

 

MyISAM:可以和其他字段一起建立联合索引。引擎的自动增长列必须是索引,如果是组合索引,自动增长可以不是第一列,他可以根据前面几列进行排序后递增。

InnoDB:InnoDB中必须包含只有该字段的索引。引擎的自动增长列必须是索引,如果是组合索引也必须是组合索引的第一列。

 

6、 表锁差异

 

MyISAM:只支持表级锁,用户在操作myisam表时,select,update,delete,insert语句都会给表自动加锁,如果加锁以后的表满足insert并发的情况下,可以在表的尾部插入新的数据。

InnoDB:支持事务和行级锁,是innodb的最大特色。行锁大幅度提高了多用户并发操作的新能。但是InnoDB的行锁,只是在WHERE的主键是有效的,非主键的WHERE都会锁全表的。

 

7、 全文索引

 

MyISAM:支持 FULLTEXT类型的全文索引

InnoDB:不支持FULLTEXT类型的全文索引,但是innodb可以使用sphinx插件支持全文索引,并且效果更好。

 

8、 表主键

 

MyISAM:允许没有任何索引和主键的表存在,索引都是保存行的地址。

InnoDB:如果没有设定主键或者非空唯一索引,就会自动生成一个6字节的主键(用户不可见),数据是主索引的一部分,附加索引保存的是主索引的值。

 

9、 表的具体行数

 

MyISAM:保存有表的总行数,如果select count(*) from table;会直接取出出该值

InnoDB:没有保存表的总行数,如果使用select count(*) from table;就会遍历整个表,消耗相当大,但是在加了where条件后,myisam和innodb处理的方式都一样。

 

10、 CURD操作

 

MyISAM:如果执行大量的SELECT,MyISAM是更好的选择。

InnoDB:如果你的数据执行大量的INSERT或UPDATE,出于性能方面的考虑,应该使用InnoDB表。DELETE 从性能上InnoDB更优,但DELETE FROM table时,InnoDB不会重新建立表,而是一行一行的删除,在innodb上如果要清空保存有大量数据的表,最好使用truncate table这个命令。

 

11、 外键

 

MyISAM:不支持

InnoDB:支持

通过上述的分析,基本上可以考虑使用InnoDB来替代MyISAM引擎了,原因是InnoDB自身很多良好的特点,比如事务支持、存储 过程、视图、行级锁定等等,在并发很多的情况下,相信InnoDB的表现肯定要比MyISAM强很多。另外,任何一种表都不是万能的,只用恰当的针对业务类型来选择合适的表类型,才能最大的发挥MySQL的性能优势。如果不是很复杂的Web应用,非关键应用,还是可以继续考虑MyISAM的,这个具体情况可以自己斟酌。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326015149&siteId=291194637