I was wondering if I could speed up a batch of queries by preparing the the Pdo outside the function like this:
$before = microtime(true);
// note this line
$query=$dbh->prepare("SELECT something FROM test WHERE record=:param");
for ($i=0 ; $i<1000 ; $i++) {
callMe("worldcup2014");
}
function callMe($param) {
global $dbh, $query;
$query->bindParam(':param', $param);
$query->execute();
$result = $query -> fetch();
}
$after = microtime(true);
echo ($after-$before) . " sec
";
And it is slightly faster than preparing inside the function like this:
$before = microtime(true);
for ($i=0 ; $i<1000 ; $i++) {
callMe("worldcup2014");
}
function callMe($param) {
global $dbh;
// line is now here inside function
$query=$dbh->prepare("SELECT something FROM test WHERE record=:param");
$query->bindParam(':param', $param);
$query->execute();
$result = $query -> fetch();
}
$after = microtime(true);
echo ($after-$before) . " sec
";
The first example takes about 0.61 secs, sometimes 0.60 secs
The second example takes about 0.65secs, never less than 0.63 secs.
I know it is a minor difference, but are there any other things that should be considered to speed up Pdo queries ? Note: I can't store/cache results. Need to grab fresh data.