Home >Database >Mysql Tutorial >What is the difference between MyISAM and InnoDB in mysql

What is the difference between MyISAM and InnoDB in mysql

青灯夜游
青灯夜游Original
2022-11-08 19:42:5933985browse

Differences: 1. InnoDB supports transactions, but MyISAM does not support it; 2. InnoDB supports foreign keys, but MyISAM does not support it; 3. InnoDB is a clustered index, while MyISAM is a non-clustered index; 4. Innodb does not support full-text indexes , while MyISAM supports full-text index; 5. InnoDB supports table and row-level locks, while MyISAM supports table-level locks; 6. InnoDB tables must have unique indexes, but Myisam does not need them; 7. The storage files are different.

What is the difference between MyISAM and InnoDB in mysql

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

InnoDB: MySQL’s default transactional engine is also the most important and widely used storage engine. It is designed to have a large number of short-term transactions. In most cases, short-term transactions are submitted normally and are rarely rolled back. InnoDB's performance and automatic crash recovery features make it popular for non-transactional storage needs. Unless there is a very special reason to use another storage engine, the InnoDB engine should be given priority.

MyISAM: In MySQL 5.1 and earlier versions, MyISAM is the default engine. MyISAM provides a large number of features, including full-text indexing, compression, spatial functions (GIS), etc., but MyISAM does not support transactions and row-level locks, and an undoubted flaw is that it cannot be safely recovered after a crash.

The difference between MyISAM and InnoDB in mysql:

1. InnoDB supports transactions, but MyISAM does not. For every SQL statement in InnoDB By default, languages ​​are encapsulated into transactions and automatically submitted, which will affect the speed, so it is best to put multiple SQL languages ​​between begin and commit to form a transaction;

2. InnoDB Supports foreign keys, while MyISAM does not. Converting an InnoDB table containing foreign keys to MYISAM will fail;

3. InnoDB is a clustered index, using B Tree as the index structure, and the data file is tied to the (primary key) index. Together (the table data file itself is an index structure organized by B Tree), there must be a primary key, and indexing through the primary key is very efficient. However, the auxiliary index requires two queries, first to query the primary key, and then to query the data through the primary key. Therefore, the primary key should not be too large, because if the primary key is too large, other indexes will also be large.

MyISAM is a non-clustered index and also uses B Tree as the index structure. The index and data files are separated, and the index saves the pointer of the data file. Primary key indexes and secondary indexes are independent.

That is to say: the leaf nodes of InnoDB's B-tree primary key index are data files, and the leaf nodes of the auxiliary index are the values ​​of the primary key; while the leaf nodes of MyISAM's B-tree primary key index and auxiliary index are both data files. address pointer.

What is the difference between MyISAM and InnoDB in mysql

What is the difference between MyISAM and InnoDB in mysql

4. InnoDB does not save the specific number of rows in the table, and the entire table is required when executing select count(*) from table scanning. MyISAM uses a variable to save the number of rows in the entire table. When executing the above statement, you only need to read the variable, which is very fast (note that no WHERE conditions can be added);

So why? What if InnoDB doesn't have this variable?

Because of the transaction characteristics of InnoDB, the number of rows in the same time table is different for different transactions, so the count statistics will calculate the number of rows that can be counted for the current transaction, not Store the total number of rows for quick query. InnoDB will try to traverse the smallest possible index unless the optimizer prompts to use another index. If the secondary index does not exist, InnoDB will also try to traverse other clustered indexes.
If the index is not completely in the buffer maintained by InnoDB (Buffer Pool), the count operation will be more time-consuming. You can create a table that records the total number of rows and let your program update the corresponding data during INSERT/DELETE. Like the problem mentioned above, this solution is not very useful if there are multiple transactions at this time. If the approximate row value is enough to meet your needs, you can try SHOW TABLE STATUS

5. Innodb does not support full-text indexing, but MyISAM supports full-text indexing. In terms of query efficiency in the field of full-text indexing MyISAM is faster and faster; PS: InnoDB after 5.7 supports full-text indexing

6. MyISAM tables can be compressed for query operations

7. InnoDB supports table and row (default) level locks, while MyISAM supports table-level locks

InnoDB’s row lock is implemented on the index, rather than locking on the physical row record. The subtext is that if the access does not hit the index and the row lock cannot be used, it will degenerate into a table lock.

For example:

t_user(uid, uname, age, sex) innodb;
 
    uid PK
    无其他索引
    update t_user set age=10 where uid=1;             命中索引,行锁。
 
    update t_user set age=10 where uid != 1;           未命中索引,表锁。
 
    update t_user set age=10 where name='chackca';    无索引,表锁。

8. The InnoDB table must have a unique index (such as a primary key) (if the user does not specify it, he or she will find/produce a hidden column Row_id to serve as the default primary key). And Myisam can not have it

9. Innodb storage files include frm and ibd, while Myisam is frm, MYD, MYI

Innodb: frm is Table definition file, ibd is the data file

Myisam: frm is the table definition file, myd is the data file, myi is the index file

##How to choose:

1. Do you want to support transactions? If so, please choose innodb. If not, you can consider MyISAM;

2. If most of the tables are only read queries, you can consider MyISAM. If there are both reads and writes, please use InnoDB.

3. After the system crashes, it will be more difficult for MyISAM to recover, is it acceptable?

4. Innodb has been released since MySQL 5.5 version Becoming the default engine of Mysql (previously MyISAM) shows that its advantages are obvious to all. If you don’t know what to use, then use InnoDB, at least it will not be bad.

Why does InnoDB recommend using auto-incrementing ID as the primary key?

Answer: Auto-incrementing ID can ensure that the B index is expanded from the right every time it is inserted, which can avoid B-tree and frequent merging and splitting (compared to using UUID). If you use string primary keys and random primary keys, data will be inserted randomly and the efficiency will be poor.

The 4 major features of innodb engine

## Insert buffer, double write , adaptive hash index (ahi), read ahead (read ahead)

[Related recommendations:

mysql video tutorial

]

The above is the detailed content of What is the difference between MyISAM and InnoDB in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Does mysql have indexes?Next article:Does mysql have indexes?