1,事务处理两条更新成功的执行速度
$fp = fopen('test_success.csv','w+');
for($i=0; $i<10; $i++){
$start = $this->microtime_float();
DB::beginTransaction();
DB::update('update resource set name = ? where id = ?', ['资源'.$i,1]);
DB::update('update resource set name = ? where id = ?', ['资源'.$i,2]);
DB::commit();
$complete = $this->microtime_float();
echo 'used '.round(($complete-$start), 2).' s'.PHP_EOL;
$str = 'used '.round(($complete-$start), 2).' s';
fputcsv($fp, [$str]);
}
fclose($fp);
// 耗费时间分别是:
"0.39 s"
"0.41 s"
"0.14 s"
"0.14 s"
"0.15 s"
"0.14 s"
"0.14 s"
"0.14 s"
"0.14 s"
"0.14 s"
2,事务处理两条,一条更新成功,另一条失败,回滚的执行速度
$fp = fopen('test_fail.csv','w+');
for($i=0; $i<10; $i++){
$start = $this->microtime_float();
DB::beginTransaction();
DB::update('update resource set name = ? where id = ?', ['资源'.$i,1]);
DB::update('update resource set name = ? where id = ?', ['资源'.$i,3]);
DB::commit();
$complete = $this->microtime_float();
echo 'used '.round(($complete-$start), 2).' s'.PHP_EOL;
$str = round(($complete-$start), 2).' s'.PHP_EOL;
fputcsv($fp, [$str]);
}
fclose($fp);
// 耗费时间分别是:
"0.52 s"
"0.26 s"
"0.2 s"
"0.19 s"
"0.19 s"
"0.19 s"
"0.2 s"
"0.19 s"
"0.19 s"
"0.19 s"
3,非事务执行成功效率
$fp = fopen('normal_success.csv','w+');
for($i=0; $i<10; $i++){
$start = $this->microtime_float();
DB::update('update resource set name = ? where id = ?', ['资源'.$i,1]);
DB::update('update resource set name = ? where id = ?', ['资源'.$i,2]);
$complete = $this->microtime_float();
echo 'used '.round(($complete-$start), 2).' s'.PHP_EOL;
$str = round(($complete-$start), 2).' s';
fputcsv($fp, [$str]);
}
fclose($fp);
//耗费时间分别是:
"0.45 s"
"0.13 s"
"0.14 s"
"0.15 s"
"0.14 s"
"0.13 s"
"0.13 s"
"0.13 s"
"0.13 s"
"0.13 s"
4,非事务执行,一个成功一个失败效率
$fp = fopen('normal_fail.csv','w+');
for($i=0; $i<10; $i++){
$start = $this->microtime_float();
DB::update('update resource set name = ? where id = ?', ['资源'.$i,1]);
DB::update('update resource set name = ? where id = ?', ['资源'.$i,3]);
$complete = $this->microtime_float();
echo 'used '.round(($complete-$start), 2).' s'.PHP_EOL;
$str = round(($complete-$start), 2).' s';
fputcsv($fp, [$str]);
}
fclose($fp);
耗费时间分别是:
"0.33 s"
"0.1 s"
"0.1 s"
"0.11 s"
"0.09 s"
"0.09 s"
"0.09 s"
"0.1 s"
"0.11 s"
"0.1 s"