Here are the results of a little PHP bench I did :
I m trying to insert 3000 records in 3 different ways, using PHP 8.0, MySQL 8.1 (mysqli)
Multiple insert queries, with multiple transaction :
$start = microtime(true);
for($i = 0; $i < 3000; $i++)
{
mysqli_query($res, "insert into app__debuglog VALUE (null,now(), msg : $i , callstack , user , debug_speed , vars )");
}
$end = microtime(true);
echo "Took " . ($end - $start) . " s
";
Did it 5 times, average : 11.132s (+/- 0.6s)
Multiple insert queries, with single transaction :
$start = microtime(true);
mysqli_begin_transaction($res, MYSQLI_TRANS_START_READ_WRITE);
for($i = 0; $i < 3000; $i++)
{
mysqli_query($res, "insert into app__debuglog VALUE (null,now(), msg : $i , callstack , user , debug_speed , vars )");
}
mysqli_commit($res);
$end = microtime(true);
echo "Took " . ($end - $start) . " ms
";
Result with 5 tests : 0.48s (+/- 0.04s)
Single aggregated insert query
$start = microtime(true);
$values = "";
for($i = 0; $i < 3000; $i++)
{
$values .= "(null,now(), msg : $i , callstack , user , debug_speed , vars )";
if($i !== 2999)
$values .= ",";
}
mysqli_query($res, "insert into app__debuglog VALUES $values");
$end = microtime(true);
echo "Took " . ($end - $start) . " ms
";
Result with 5 tests : 0.085s (+/- 0.05s)
So, for a 3000 row insert, looks like :
- Using multiple queries in a single write transaction is ~22 times faster than making a multiple queries with multiple transactions for each insert.
- Using a single aggregated insert statement is still ~6 times faster than using multiple queries with a single write transaction