如何使用带有 PHP PDO 的异步 Mysql 查询

How to use async Mysql query with PHP PDO

Mysqlnd驱动PHP5.6有机会使用Async查询http://php.net/manual/en/mysqli.reap-async-query.php

如何在 PDO 中使用异步查询?

无效,代码 (PHP asynchronous mysql-query):

$dbConnectionOne = new \PDO($cnn0, $conf['user'], $conf['pass']);
$dbConnectionOne->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

$dbConnectionTwo =  new \PDO($cnn0, $conf['user'], $conf['pass']);
$dbConnectionTwo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$dbConnectionTwo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);


$t = time();
$synchStmt = $dbConnectionOne->prepare('SELECT sleep(2)');
$synchStmt->execute();

$asynchStmt = $dbConnectionTwo->prepare('SELECT sleep(1)');
$asynchStmt->execute();

$measurementConfiguration = array();
foreach ($synchStmt->fetchAll() as $synchStmtRow) {
   print_r($synchStmtRow);
}

while (($asynchStmtRow = $asynchStmt->fetch()) !== false) {
   print_r($asynchStmtRow);
}


$t = time() - $t;

echo 'query execute ', $t, ' sec',PHP_EOL;

排除 2 秒但结果 = 3 秒

没有。您不能对 PDO 使用 Mysql 异步查询。 Mysql我是唯一的选择。

您可以为此使用 mysqli_multi_query 或常规 query/poll/reap 序列。