Shouldn't prepared statements be much more fsater?
Posted
by silversky
on Stack Overflow
See other posts from Stack Overflow
or by silversky
Published on 2010-04-13T00:15:48Z
Indexed on
2010/04/13
0:22 UTC
Read the original article
Hit count: 707
$s = explode (" ", microtime());
$s = $s[0]+$s[1];
$con = mysqli_connect ('localhost', 'test', 'pass', 'db') or die('Err');
for ($i=0; $i<1000; $i++) {
$stmt = $con -> prepare( " SELECT MAX(id) AS max_id , MIN(id) AS min_id FROM tb ");
$stmt -> execute();
$stmt->bind_result($M,$m);
$stmt->free_result();
$rand = mt_rand( $m , $M ).'<br/>';
$res = $con -> prepare( " SELECT * FROM tb WHERE id >= ? LIMIT 0,1 ");
$res -> bind_param("s", $rand);
$res -> execute();
$res->free_result();
}
$e = explode (" ", microtime());
$e = $e[0]+$e[1];
echo number_format($e-$s, 4, '.', '');
// and:
$link = mysql_connect ("localhost", "test", "pass") or die ();
mysql_select_db ("db") or die ("Unable to select database".mysql_error());
for ($i=0; $i<1000; $i++) {
$range_result = mysql_query( " SELECT MAX(`id`) AS max_id , MIN(`id`) AS min_id FROM tb ");
$range_row = mysql_fetch_object( $range_result );
$random = mt_rand( $range_row->min_id , $range_row->max_id );
$result = mysql_query( " SELECT * FROM tb WHERE id >= $random LIMIT 0,1 ");
}
defenitly prepared statements are much more safer but also every where it says that they are much faster BUT in my test on the above code I have: - 2.45 sec for prepared statements - 5.05 sec for the secon example
What do you think I'm doing wrong? Should I use the second solution or I should try to optimize the prep stmt?
© Stack Overflow or respective owner