Innodb 和 Myisam 区别

1、InnoDB 前者支持外键和事务,而 MyISAM 并不支持

2、事务性表应该使用 InnoDB,频繁读取如select操作很频繁的应该使用 MyISAM 引擎


API

查看数据库的引擎支持信息

show ENGINES

查看该库用所有表的基本状态包括引擎

show table status from < 数据库名 >

修改表的引擎

ALTER table < 表名 > engine < MyISAM / InnoDB >


创建测试表

新建 user_myisam 表,引擎为 MyISAM

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `user_sys`
-- ----------------------------
DROP TABLE IF EXISTS `user_myisam`;
CREATE TABLE `user_myisam` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_pwd` varchar(30) NOT NULL,
  `user_name` varchar(30) NOT NULL,
  `user_regdate` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

新建 user_innodb 表,引擎为 InnoDB

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `user_innodb`
-- ----------------------------
DROP TABLE IF EXISTS `user_innodb`;
CREATE TABLE `user_innodb` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_pwd` varchar(30) NOT NULL,
  `user_name` varchar(30) NOT NULL,
  `user_regdate` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


插入数据测试

1、新建存储过程addUser

CREATE DEFINER=`root`@`localhost` PROCEDURE `addUser`(IN t int, IN n int)
BEGIN
    SET @num := 1;
    WHILE @num < n DO
        IF t = 1 THEN
            INSERT INTO user_innodb(user_name, user_pwd) VALUES (CONCAT('user',@num), '123');
        ELSE 
            INSERT INTO user_myisam(user_name, user_pwd) VALUES (CONCAT('user',@num), '123');
        END IF;
        SET @num := @num + 1; 
    END WHILE;
END

2、InnoDB 插入 100W 条数据测试,耗时 2613 秒

3、MyISAM 插入 100W 条数据测试, 耗时 91 秒

很显然,MyISAM 的插入速度 比 InnoDB 快很多

所以如果我们有导入数据的操作,可以先把表改为 MyISAM ,等导入完成之后再改为 InnoDB。


查询操作测试

两表同时进行100W的查询操作。测试耗时

select * from user_innodb;
select * from user_myisam;

结果表示: innodb 耗时 1.2 秒, myisam 耗时 0.8 秒

results matching ""

    No results matching ""