Innodb/MyISAM在自增/UUID主键下的性能与索引空间比较

自增建表:

CREATE TABLE `auto_test_innodb/myisam` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `a` varchar(64) NOT NULL,
  `b` varchar(64) NOT NULL,
  `c` int(11) NOT NULL,
  `d` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `a` (`a`),
  KEY `b` (`b`),
  KEY `c` (`c`),
  KEY `d` (`d`)
) ENGINE=innodb/myisam  DEFAULT CHARSET=latin1;

uuid建表:

CREATE TABLE `uuid_test_${engine}` (
  `id` varchar(64) NOT NULL,
  `a` varchar(64) NOT NULL,
  `b` varchar(64) NOT NULL,
  `c` int(11) NOT NULL,
  `d` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `a` (`a`),
  KEY `b` (`b`),
  KEY `c` (`c`),
  KEY `d` (`d`)
) ENGINE=innodb/myisam DEFAULT CHARSET=latin1;

测试用存储过程:

CREATE PROCEDURE `uuid_auto_test`(IN rows int)
begin
declare i int;
set i=0;
while i
insert into uuid/auto_test_innodb/myisam ([id],a,b,c,d) select [uuid()],uuid(),uuid(),RAND()*1000,RAND()*1000;
set i=i+1;
end while;
end

测试结果:

                    innodb          myisam            
100W    auto    110s    113m    106s    92m     
100W    uuid    124s    143m    119s    118m   
300W    auto    337s    336m    382s    275m    
300W    uuid    381s    428m    429s    355m

结论:

  • 从测试结果可见,uuid做主键效率要比自增低10%-20%左右,由于实际生产环境的表字段更多,用uuid做主键插入会导致更多的页分裂,所以实际效率可能更低。
  • MyISAM索引占用空间更少,这是由于MyISAM索引和数据文件分离,索引通过前缀压缩的方式减少了占用空间。
  • 不过,此测试没有很好解释维护非聚簇索引队innodb插入的影响,因为innodb非聚簇索引叶子节点保存的是行的主键值,理论上主键值约复杂,维护成本越高。
  • MyISAM的数据分布式按照数据插入的顺序存储在磁盘上,理论上插入效率应该不受uuid或自增做主键而影响,实际效率仍旧是自增较高。

你可能感兴趣的:(性能测试)