I'm in the middle of updating/reworking some database code and I was wondering, what I should really expect from using prepared statements.
Take this example code:
$values = '';
for ($i = 0; $i < $count; $i++) {
$name = mysql_real_escape_string ($list[$i][1]);
$voc = mysql_real_escape_string ($list[$i][3]);
$lev = $list[$it][2];
$lev = is_numeric ($lev)? $lev : 0;
$values .= ($values == '')? "('$name', '$voc', $lev)" : ", ('$name', '$voc', $lev)";
}
if ($values != '') {
$core->query ("INSERT INTO onlineCList (name, voc, lev) VALUES $values;");
}
Now, apart from the obvious gain in readability (, sanity) and the fact that max_packet_size
stops being an issue, am I supposed to expect any changes in performance when I recode this to use prepared statements? I'm connecting remotely to the MySQL server, and I worry that sending multiple small packets would be significantly slower then sending one big packet. If this is the case, can MySQLi/mysqlnd cache these packets?
Another example:
$names = '';
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$name = mysql_real_escape_string($row['name']);
$names .= ($names == '') ? "'$name'" : ", '$name'";
}
if ($names != '') {
$core->query ("UPDATE onlineActivity SET online = NULL WHERE name IN ($names) AND online = 1;");
}
As above, should I expect the unexpected, after recoding this to use prepared statements? Does it make any difference for the MySQL server, if it has to run one query with a big IN clause, or multiple prepared queries with equality checks (.. WHERE name = $name AND ..
)?
Assume that everything is properly indexed.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…