I'm working on HHVM with MySQL. And I'm really confused to find out that batching 2000 sql queries using multi_query() is much slower than a 2000 loop with single query() (please see the code and the result at end). By further profiling, I found the API next_result() takes most of the time (~70%).
My questions are:
(1) Why the API next_result() is so slow?
(2) If I want to do thousands of sql queries together, is there any way better than a naive loop?
Thanks!
Here is the code (php):
$conn = new mysqli("localhost", "root", "pwd", "table");
$loop = 2000;
$q = "select * from ContactInfo;";
// single query in a loop
$results = array();
$sq_start = microtime(true);
for ($i=0; $i < $loop; $i++) {
$ret = $conn->query($q);
$results[] = $ret;
}
for ($i=0; $i < $loop; $i++) {
$xx = $results[$i]->fetch_all();
}
$sq_end = microtime(true);
// construct the multi-query
for ($i=0; $i < $loop; $i++) {
$m_q .= $q;
}
// multi-query in one round-trip
$mq_start = microtime(true);
$conn->multi_query($m_q);
do {
$ret = $conn->store_result();
$xx = $ret->fetch_all();
} while($conn->next_result());
$mq_end = microtime(true);
echo "Single query: " . ($sq_end - $sq_start)*1000 . " ms\n";
echo "Multi query: " . ($mq_end - $mq_start)*1000 . " ms\n";
The result is following:
Single query: 526.38602256775 ms
Multi query: 1408.7419509888 ms
Note: next_result() will consume 922ms in this case.