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.使用建议

以下两点必须使用 InnoDB:
1)可靠性高或者要求事务处理,则使用InnoDB。这个是必须的。
2)表更新和查询都相当的频繁,并且表锁定的机会比较大的情况指定InnoDB数据引擎的创建。
对比之下,MyISAM的使用场景:
1)做很多count的计算的。如一些日志,调查的业务表。
2)插入修改不频繁,查询非常频繁的。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值