我有两张数据结果完全一样的表,但引擎不相同,一个是InnoDB,一个是MyISAM
表结果如下:
CREATE TABLE `innodb_test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(128) DEFAULT '',
`age` tinyint(3) unsigned DEFAULT '0',
`updateon` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`createon` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
CREATE TABLE `innodb_test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(128) DEFAULT '',
`age` tinyint(3) unsigned DEFAULT '0',
`updateon` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`createon` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
<?php
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=test', 'root', 'xxxxx');
$start = microtime(true);
$now = date('Y-m-d H:i:s');
$table_name = 'innodb_test';
for($i = 1; $i < 10000; $i++ ) {
$name = 'A'.$i;
$age = rand(1,100);
$sql = "INSERT INTO `$table_name` (`name`,`age`,`updateon`,`createon`) values ('$name',$age,'$now','$now')";
$pdo->exec($sql);
}
$end = microtime(true);
echo "start=$start end=$end, total=" . $end-$start;
InnoDB执行结果:1446018429.6891-1446018535.7919=106.10283517838
MyISAM执行结果:1446018647.4344-1446018648.7329=1.298492193222
很惊讶,居然相差那么大!!经过多次验证,还是如此。。。疑问!
原来,这涉及到innodb的参数配置 innodb_flush_log_at_trx_commit。
关于innodb_flush_log_at_trx_commit配置,请看Innodb参数innodb_flush_log_at_trx_commit详解