PHP MySQL的异步查询 [英] PHP asynchronous mysql-query

查看:186
本文介绍了PHP MySQL的异步查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序首先查询2大组数据,然后做的第一组数据的一些工作,用,它在第二。

My app first queries 2 large sets of data, then does some work on the first set of data, and "uses" it on the second.

如果可能,我想它,而不是只查询第一套同步和异步第二,做第一组的工作,然后等待第二组的查询来完成,如果有尚未最后使用第一组数据就可以了。

If possible I'd like it to instead only query the first set synchronously and the second asynchronously, do the work on the first set and then wait for the query of the second set to finish if it hasn't already and finally use the first set of data on it.

这是可能以某种方式?

推荐答案

MySQL的要求,一个连接里面,查询完全处理的<一个href=\"https://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html#error_cr_commands_out_of_sync\">before下一个查询启动。这包含的所有的结果的取

MySQL requires that, inside one connection, a query is completely handled before the next query is launched. That includes the fetching of all results.

有可能的,但是,对


  • 在一次取一个,而不是所有的结果有一个

  • 通过建立多个连接启动多个查询

在默认情况下,PHP会等到的所有的结果出来,然后在内部(在MySQL驱动程序)一次获取所有结果。 这是真实的,甚至的使用,例如当 PDOStatement对象::获取()来一次导入他们在您的code一行。当使用PDO,这可能是与设置属性<一pvented $ P $ href=\"http://php.net/manual/en/ref.pdo-mysql.php\"><$c$c>\\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY。这是非常有用的:

By default, PHP will wait until all results are available and then internally (in the mysql driver) fetch all results at once. This is true even when using for example PDOStatement::fetch() to import them in your code one row at a time. When using PDO, this can be prevented with setting attribute \PDO::MYSQL_ATTR_USE_BUFFERED_QUERY to false. This is useful for:

  • speeding up the handling of query results: your code can start processing as soon as one row is found instead of only after every row is found.
  • working with result sets that are potentially larger than the memory available to PHP (PHP's self-imposed memory_limit or RAM memory).

注意,常速度是由一个存储系统与意味着总的处理时间为两个查询在同一时间由一个运行它们之一,当比运行时,其中较大的特性的限制。

Be aware that often the speed is limited by a storage system with characteristics that mean that the total processing time for two queries is larger when running them at the same time than when running them one by one.

这是实施例(其可以完全在MySQL中进行,但为表示概念...):

An example (which can be done completely in MySQL, but for showing the concept...):

$dbConnectionOne = new \PDO('mysql:hostname=localhost;dbname=test', 'user', 'pass');
$dbConnectionOne->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

$dbConnectionTwo = new \PDO('mysql:hostname=localhost;dbname=test', 'user', 'pass');
$dbConnectionTwo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$dbConnectionTwo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

$synchStmt = $dbConnectionOne->prepare('SELECT id, name, factor FROM measurementConfiguration');
$synchStmt->execute();

$asynchStmt = $dbConnectionTwo->prepare('SELECT measurementConfiguration_id, timestamp, value FROM hugeMeasurementsTable');
$asynchStmt->execute();

$measurementConfiguration = array();
foreach ($synchStmt->fetchAll() as $synchStmtRow) {
    $measurementConfiguration[$synchStmtRow['id']] = array(
        'name' => $synchStmtRow['name'],
        'factor' => $synchStmtRow['factor']
    );
}

while (($asynchStmtRow = $asynchStmt->fetch()) !== false) {
    $currentMeasurementConfiguration = $measurementConfiguration[$asynchStmtRow['measurementConfiguration_id']];
    echo 'Measurement of sensor ' . $currentMeasurementConfiguration['name'] . ' at ' . $asynchStmtRow['timestamp'] . ' was ' . ($asynchStmtRow['value'] * $currentMeasurementConfiguration['factor']) . PHP_EOL;
}

这篇关于PHP MySQL的异步查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆