The storage engine InnoDB and MySQL MyISAM explain the difference between the bottom and (rpm)

Digression: broad depth of Chinese culture, learn from Java to the database, all manifestations of the same components can not have both fish and bear's essence. Naturally, programming, security and efficiency at the same time it is difficult to be perfect, and this time let me InnoDB and MyISAM eye-opener.

好了,Talk is cheap,show you the code:

Test environment: Mysql 5.7.20-log, IDEA 2018

First create two tables: testinnodb, testmyisam, sql as follows:

 
 

1. At the same time large quantities of data inserted (one million, Million), a small series using stored procedures, codes and test results are as follows:

The following code runs on IDEA can:

 
 

100W of data while inserting, MyISAM takes about 38S, and it took about 76 minutes InnoDB 4s, it is obvious that InnoDB MyISAM win in the processing speed, but if the actual items used, as it relates to data security (or things security) issues, most companies chose InnoDB, few companies use MyISAM (effective in its strict control of the business layer). MyISAM but we can still be used in the log data analysis, experiments and other environments.

2. Look at which contrast search terms censored

 
 

Change search consuming

 
 

Delete consuming

In fact, contrast down gap is not inserted into the data as exaggerated, most of the requirements for the safety of things companies can still acceptable.

PS: You can use the mysql plug-in profile to show the duration of the most recently executed command usage is as follows:

mysql is off by default profiles, you need to open it,

Check whether to open the command: show variables like '% pro%';

 
 

Xiao Bian has put his open, so the display is ON, the default is OFF.

Open command: set profiling = 1;

Close command: set profiling = 0;

Information on the latest long use the command: show profiles;

According Query ID Query a single command Details: show profile for query 1;

根据Query ID查询单个命令memory,source,cpu等详情: show profile cpu for query 1;或者 show profile all for query 1;

测试Over,接下来总结一下:

1.InnoDB支持事物,外键等高级的数据库功能,MyISAM不支持。需要注意的是,InnDB行级锁也不是绝对的,例如mysql执行一个未定范围的sql时,也还是会锁表,例如sql中like的使用

2.效率,明显MyISAM在插入数据的表现是InnoDB所远远不及的,在删改查,随着InnoDB的优化,差距渐渐变小

3.行数查询,InnoDB不保存行数,也就是select的时候,要扫描全表,MyISAM只需读取保存的行数即可,这也是MyISAM查询速度快的一个因素。

4.索引,InnoDB会自动创建Auto_Increment类型字段的索引,一般习惯应用于主键,即主键索引(只包含该字段),而MyISAM可以和其他字段创建联合索引。

除此之外,MyISAM还支持全文索引(FULLTEXT_INDEX),压缩索引,InnoDB不支持。

备注:MyISAM的索引和数据是分开的,并且索引是有压缩的,内存使用率就对应提高了不少。能加载更多索引,而Innodb是索引和数据是紧密捆绑的,没有使用压缩从而会造成Innodb比MyISAM体积庞大不小。

InnoDB存储引擎被完全与MySQL服务器整合,InnoDB存储引擎为在主内存中缓存数据和索引而维持它自己的缓冲池。InnoDB存储它的表&索引在一个表空间中,表空间可以包含数个文件(或原始磁盘分区)。这与MyISAM表不同,比如在MyISAM表中每个表被存在分离的文件中。InnoDB 表可以是任何尺寸,即使在文件尺寸被限制为2GB的操作系统上。

5.服务器数据备份。InnoDB必须导出SQL来备份,LOAD TABLE FROM MASTER操作对InnoDB是不起作用的,解决方法是首先把InnoDB表改成MyISAM表,导入数据后再改成InnoDB表,但是对于使用的额外的InnoDB特性(例如外键)的表不适用。

备注:而且MyISAM应对错误编码导致的数据恢复速度快。MyISAM的数据是以文件的形式存储,所以在跨平台的数据转移中会很方便。在备份和恢复时可单独针对某个表进行操作。

InnoDB是拷贝数据文件、备份 binlog,或者用 mysqldump,支持灾难恢复(仅需几分钟),MyISAM不支持,遇到数据崩溃,基本上很难恢复,所以要经常进行数据备份。

6.锁的支持。**MyISAM只支持表锁。InnoDB支持表锁、行锁 行锁大幅度提高了多用户并发操作的新能。但是InnoDB的行锁,只是在WHERE的主键是有效的,非主键的WHERE都会锁全表的

使用场景建议:

1)可靠性高或者要求事务处理,则使用InnoDB。这个是必须的。

2)表更新和查询都相当的频繁,并且表锁定的机会比较大的情况指定InnoDB数据引擎的创建。

对比之下,MyISAM的使用场景:

1)做很多count的计算的。如一些日志,调查的业务表。

2)插入修改不频繁,查询非常频繁的。

MySQL能够允许你在表这一层应用数据库引擎,所以你可以只对需要事务处理的表格来进行性能优化,而把不需要事务处理的表格交给更加轻便的MyISAM引擎。对于 MySQL而言,灵活性才是关键。

引擎原理分析

MyISAM索引结构: MyISAM索引用的B+ tree来储存数据,MyISAM索引的指针指向的是键值的地址,地址存储的是数据。B+Tree的数据域存储的内容为实际数据的地址,也就是说它的索引和实际的数据是分开的,只不过是用索引指向了实际的数据,这种索引就是所谓的非聚集索引

主索引如下:

 
 

辅助索引如下:

 
 

因此,过程为: MyISAM中索引检索的算法为首先按照B+Tree搜索算法搜索索引,如果指定的Key存在,则取出其data域的值,然后以data域的值为地址,根据data域的值去读取相应数据记录。

InnoDB引擎的索引结构:

也是B+Treee索引结构。Innodb的索引文件本身就是数据文件,即B+Tree的数据域存储的就是实际的数据,这种索引就是聚集索引。这个索引的key就是数据表的主键,因此InnoDB表数据文件本身就是主索引。【Java高架构师、分布式架构、高可扩展、高性能、高并发、性能优化、Spring boot、Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分布式项目实战学习架构师视频免费获取架构群;855355016】

InnoDB的辅助索引数据域存储的也是相应记录主键的值而不是地址,所以当以辅助索引查找时,会先根据辅助索引找到主键,再根据主键索引找到实际的数据。所以Innodb不建议使用过长的主键,否则会使辅助索引变得过大。

建议使用自增的字段作为主键,这样B+Tree的每一个结点都会被顺序的填满,而不会频繁的分裂调整,会有效的提升插入数据的效率。

主索引如下:

 
 

辅助索引如下:

 
 

上图,可以看到叶节点包含了完整的数据记录。这种索引叫做聚集索引。因为InnoDB的数据文件本身要按主键聚集,所以InnoDB要求表必须有主键(MyISAM可以没有),如果没有显式指定,则MySQL系统会自动选择一个可以唯一标识数据记录的列作为主键,如果不存在这种列,则MySQL自动为InnoDB表生成一个隐含字段作为主键,这个字段长度为6个字节,类型为长整形。

而且,与MyISAM索引的不同是InnoDB的辅助索引data域存储相应记录主键的值而不是地址。换句话说,InnoDB的所有辅助索引都引用主键作为data域。

Thus, the process is: the primary key organized into a B + tree, the line data is stored in the leaf node, by using "where id = 13" such conditions to find the primary key, follow search algorithm B + tree to find corresponding to the leaf node, then the data line is obtained. If the Name column of the conditional search, requires two steps: the first step in the B + tree index in the secondary retrieval Name, which reaches leaf nodes corresponding to the acquired primary key. The second step uses the primary key in the primary B + tree index perform another B + tree retrieval operation, to obtain the final leaf node entire row of data.

Neither index data search process is as follows:

 
 

Small gifts around and Jane book to my attention



Author: Java Advanced Technology
link: https: //www.jianshu.com/p/92ba79cbc6a3
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin www.cnblogs.com/dayhand/p/11080155.html