Below is the code that executed the test. The additional functions per iteration are kept constant, but the slightly variable size of data produced per iteration should resemble real-life applications. Also, random access parameters (index data and values) are to ensure minimum or no query caching so these should be direct results.
<?php
class myStmt extends mysqli_stmt {
public function __construct ($mysqli, $query) {
parent::__construct($mysqli, $query);
}
public function execute() {
parent::execute();
if ($ern= mysqli_stmt_errno($this)) {
throw new Exception (mysqli_stmt_error($this), $ern);
}
}
}
class myMySQLi extends mysqli {
public $dbQueries= 0; // Number of performed queries
function __construct($serv, $user, $pass, $db) {
parent::__construct ($serv, $user, $pass, $db);
if ($ern= mysqli_connect_errno()) {
throw new Exception (mysqli_connect_error(), $ern);
}
}
function query ($query) {
$res= parent::query ($query);
if ($ern= mysqli_errno($this)) {
$erm= mysqli_error($this);
$trace= debug_backtrace();
throw new Exception ("query('$query') called in {$trace[0]['file']}, line {$trace[0]['line']} :: $erm", $ern);
} else $this->dbQueries++;
return $res;
}
function simple_query($query) {
$res= $this->query($query);
$res->close();
}
function prepare ($query) {
$res= new myStmt ($this, $query);
if ($ern= mysqli_errno($this)) {
$erm= mysqli_error($this);
$trace= debug_backtrace();
throw new Exception ("prepare('$query') called in {$trace[0]['file']}, line {$trace[0]['line']} :: $erm", $ern);
} else $this->dbQueries++;
return $res;
}
}
function myRandomString($len) {
$str= '';
for ($i=0; $i<$len; $i++) {
$seed= mt_rand (0, 2);
switch ($seed) {
case 0: $str.= mt_rand (0, 9); break;
case 1: $str.= chr(mt_rand(0, 25)+65); break;
case 2:
default: $str.= chr(mt_rand(0, 25)+97); break;
}
}
return $str;
}
header ('Content-type: text/plain');
$n= 10000;
$db= new myMySQLi('localhost', 'username', 'password', 'database');
$db->query('TRUNCATE `test`');
$db->query('TRUNCATE `test2`');
set_time_limit(360);
echo "$n x \$db->query('INSERT INTO test (val, idx) VALUES ('\$val', \$idx)')"; flush();
$stime= microtime(true);
for ($i=0; $i<$n; $i++) {
$val= myRandomString(mt_rand(10, 200));
$idx= mt_rand(1, 600);
$res= $db->query("INSERT INTO test (val, idx) VALUES ('$val', $idx)");
}
echo "............................................................... ",sprintf('%.4f', microtime(true)-$stime),"\n";
echo "$n x Full prepared statement per loop (prepare, bind, execute, close)"; flush();
$stime= microtime(true);
for ($i=0; $i<$n; $i++) {
$idx= mt_rand(1, 600);
$val= myRandomString(mt_rand(10, 200));
$stmt= $db->prepare('INSERT INTO test (val, idx) VALUES (?, ?)');
$stmt->bind_param('si', $val, $idx);
$stmt->execute();
$stmt->close();
}
echo ".............................................................. ",sprintf('%.4f', microtime(true)-$stime),"\n";
echo "$n x Prepared statement (only execute() in loop)"; flush();
$stime= microtime(true);
$stmt= $db->prepare('INSERT INTO test (val, idx) VALUES (?, ?)');
$stmt->bind_param('si', $val, $idx);
for ($i=0; $i<$n; $i++) {
$idx= mt_rand(1, 600);
$val= myRandomString(mt_rand(10, 200));
$stmt->execute();
}
$stmt->close();
echo "................................................................................... ",sprintf('%.4f', microtime(true)-$stime),"\n";
echo "\n\n";
echo "$n x \$db->query('SELECT val FROM test WHERE idx=\$idx LIMIT 1')"; flush();
$stime= microtime(true);
for ($i=0; $i<$n; $i++) {
$idx= mt_rand(1, 600);
$res= $db->query("SELECT val FROM test WHERE idx=$idx LIMIT 1");
$row= $res->fetch_row();
$res->free_result();
}
echo "..................................................................... ",sprintf('%.4f', microtime(true)-$stime),"\n";
echo "$n x Full prepared statement per loop (prepare, bind, execute, close)"; flush();
$stime= microtime(true);
for ($i=0; $i<$n; $i++) {
$idx= mt_rand(1, 600);
$stmt= $db->prepare("SELECT val FROM test WHERE idx=? LIMIT 1");
$stmt->bind_param('i', $idx);
$stmt->execute();
$stmt->bind_result($row);
$stmt->fetch();
$stmt->free_result();
$stmt->close();
}
echo ".............................................................. ",sprintf('%.4f', microtime(true)-$stime),"\n";
echo "$n x Prepared statement (only execute(), fetch() and free_result() in loop)"; flush();
$stime= microtime(true);
$stmt= $db->prepare("SELECT val FROM test WHERE idx=? LIMIT 1");
$stmt->bind_param('i', $idx);
$stmt->bind_result($row);
for ($i=0; $i<$n; $i++) {
$idx= mt_rand(1, 600);
$stmt->execute();
$stmt->fetch();
$stmt->free_result();
}
$stmt->close();
echo "........................................................ ",sprintf('%.4f', microtime(true)-$stime),"\n";
echo "\n\n";
echo "$n x \$db->query('INSERT INTO test2 (`int1`, `int2`, `int3`, `str1`, `str2`, `idx`) VALUES (...)')"; flush();
$stime= microtime(true);
for ($i=0; $i<$n; $i++) {
$int1= mt_rand(1, 600000);
$int2= mt_rand(1, 600000);
$int3= mt_rand(1, 600000);
$str1= myRandomString(mt_rand(10, 200));
$str2= myRandomString(mt_rand(10, 200));
$idx= mt_rand(1, 100);
$res= $db->query("INSERT INTO test2 (`int1`, `int2`, `int3`, `str1`, `str2`, `idx`) VALUES ($int1, $int2, $int3, '$str1', '$str2', $idx)");
}
echo ".................................. ",sprintf('%.4f', microtime(true)-$stime),"\n";
echo "$n x Prepared statement (only execute() in loop)"; flush();
$stime= microtime(true);
$stmt= $db->prepare('INSERT INTO test2 (`int1`, `int2`, `int3`, `str1`, `str2`, `idx`) VALUES (?, ?, ?, ?, ?, ?)');
$stmt->bind_param('iiissi', $int1, $int2, $int3, $str1, $str2, $idx);
for ($i=0; $i<$n; $i++) {
$int1= mt_rand(1, 600000);
$int2= mt_rand(1, 600000);
$int3= mt_rand(1, 600000);
$str1= myRandomString(mt_rand(10, 200));
$str2= myRandomString(mt_rand(10, 200));
$idx= mt_rand(1, 100);
$stmt->execute();
}
$stmt->close();
echo "................................................................................... ",sprintf('%.4f', microtime(true)-$stime),"\n";
echo "\n\n";
echo "$n x \$db->query('SELECT `int1`, `int2`, `int3`, `str1`, `str2`, `idx` FROM test2 WHERE `idx`=\$idx LIMIT 1') -- fetch_assoc()"; flush();
$stime= microtime(true);
for ($i=0; $i<$n; $i++) {
$idx= mt_rand(1, 100);
$res= $db->query("SELECT `int1`, `int2`, `int3`, `str1`, `str2`, `idx` FROM test2 WHERE `idx`=$idx LIMIT 1");
$row= $res->fetch_assoc();
$res->free_result();
}
echo "....... ",sprintf('%.4f', microtime(true)-$stime),"\n";
echo "$n x Prepared statement (only execute(), fetch() and free_result() in loop)"; flush();
$stime= microtime(true);
$stmt= $db->prepare("SELECT `int1`, `int2`, `int3`, `str1`, `str2`, `idx` FROM test2 WHERE `idx`=? LIMIT 1");
$stmt->bind_param('i', $idx);
$stmt->bind_result($int1, $int2, $int3, $str1, $str2, $idx2);
$idx2=0;
for ($i=0; $i<$n; $i++) {
$idx= mt_rand(1, 100);
$stmt->execute();
$stmt->fetch();
$stmt->free_result();
}
$stmt->close();
echo "........................................................ ",sprintf('%.4f', microtime(true)-$stime),"\n";
?>