我需要在数据库中记录每个事务的日志,但是我注意到使用COMMIT和not using的行为不同。
当我使用commit时,循环中的每一轮实际上都是零,而当我不使用commit时,提交时间会更长。
我想知道这种行为是否正常,是否有一些方法可以准确地管理时间。
谢谢
$microtimeA = microtime( true );
$pdo->beginTransaction();
$i=1;
while($i<=5)
{
$microtimeB = microtime( true );
$stmt = $pdo-> prepare( "INSERT INTO BLOG ( TITLE ) VALUES ( $i ) ");
$stmt-> execute();
$i++;
echo "<p>" , number_format( microtime( true ) - $microtimeB , 4 ) , "</p>";
}
$pdo-> commit();
echo "<p>[" , number_format( microtime( true ) - $microtimeA , 4 ) , "]</p>";提交:
1: 0.0003
2: 0.0002
3: 0.0002
4: 0.0002
5: 0.0002
0.0228
无提交:
1: 0.0149
2: 0.0197
3: 0.0416
4: 0.0135
5: 0.0332
0.1229
发布于 2013-05-10 04:03:59
提交事务时,数据库会将数据刷新到磁盘。这通常是一个缓慢的操作,因为它需要等待旋转硬盘的至少一次旋转。
在“不提交”的情况下,您的数据库将自动提交每个事务。因此,您有5个独立的磁盘刷新。
在“提交”的情况下,您只需要刷新到磁盘一次。
数字支持这一点: 0.1229几乎是0.0228的5倍
https://stackoverflow.com/questions/16381919
复制相似问题