MySQL两种引擎MyIsam与InnoDB对比

1.在一个普通数据库中创建两张分别以MyIsam和InnoDB作为存储引擎的表:

create table testMyIsam(  
id int unsigned primary key auto_increment,  
name varchar(20) not null  
)engine=myisam;
create table testInnoDB( 
 id int unsigned primary key auto_increment, 
 name varchar(20) not null 
 )engine=innodb; 

2. 对比插入效率(十万级)

为了效率高,我们可以使用存储过程的方式。

先向testmyisam表中添加数据:
//创建存储过程
drop procedure if exists ptestmyisam;

create procedure ptestmyisam()
begin
declare pid int ;
set pid = 100000;
while pid>0 
do
insert into testmyisam(name) values(concat("fuzhu", pid));
set pid = pid-1;
end while;
end ;

//使用存储过程:
call ptestmyisam();

花费时间大概在31s左右:
在这里插入图片描述

再向testinnodb表中添加数据:
//创建存储过程
drop procedure if exists ptestinnodb;

create procedure ptestinnodb()
begin
declare pid int ;
set pid = 100000;
while pid>0 
do
insert into testinnodb(name) values(concat("fuzhu", pid));
set pid = pid-1;
end while;
end ;

//使用存储过程:
call ptestinnodb();

花费时间大概在48s左右:
在这里插入图片描述

虽然速度上MyISAM快,但是增删改是涉及事务安全的,所以用InnoDB相对好很多。当然innodb默认是开启事务的,如果我们把事务给停了,会快很多。

//停掉事务
set autocommit = 0;  
//调用存储过程
call ptestInndb; 
//重启事务
set autocommit = 1; 

只需要13.8s
在这里插入图片描述

3.

猜你喜欢

转载自blog.csdn.net/asleepysheep/article/details/86185275