$reader_hosts = array( "192.88.225.243", "192.88.225.242", "192.88.225.240", "192.88.225.160", "192.88.225.159" );
$all_links=array();
# Establish connections
foreach ($reader_hosts as $i) {
$mysqli = new mysqli($i, 'root', '', 'test');
if ($mysqli->connect_error) {
echo 'Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error;
}else{
$all_links[]=$mysqli;
$mysqli->query("SET wsrep_causal_reads=1");
}
}
# Run queries in parallel:
foreach ($all_links as $linkid => $link) {
$link->query("SELECT something FROM tableN WHERE ", MYSQLI_ASYNC);
}
$processed = 0;
do {
$links = $errors = $reject = array();
foreach ($all_links as $link) {
$links[] = $errors[] = $reject[] = $link;
}
# loop to wait on results
if (!mysqli_poll($links, $errors, $reject, 60)) {
continue;
}
foreach ($links as $k=>$link) {
if ($result = $link->reap_async_query()) {
$res = $result->fetch_row();
# Handle returned result
mysqli_free_result($result);
} else die(sprintf("MySQLi Error: %s", mysqli_error($link)));
$processed++;
}
} while ($processed < count($all_links));
This reminds me of Cassandra’s multiget() and non-blocking socket with select()
Will be interesting for what practical purpose this can be used.
If MySQLnd was coupled with a UI that allowed for data to be pushed via websockets, then it would be a very interesting mechanism for a single request to initiate multiple different fetches and actions, all of which could be returned asynchronously over the socket to a waiting handler.
Great article. We’ve been looking for a way to do this, I wonder if this is available with PDO interface for php/mySQL?
It would seem this would be great for de-aggregating queries of the following type where the final number of rows is small, but the aggregation is terribly slow and could be better done in an array to avoid filesorts and to execute the sub-queries in parallel.