PHP/MySQL - 同时进行多个查询 [英] PHP/MySQL - Multiple queries at the same time

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

问题描述

我有 24 个数据库,其中一个表标记为 email_queue.

I have 24 databases with a table labeled email_queue.

我有另一个数据库,其中列出了包含 email_queue 表的所有数据库.

I have another database with a list of all the databases that have the email_queue table in it.

我遍历数据库列表并查询我的 email_queue 表以发送每个数据库的邮件.

I loop through the list of databases and query my email_queue table to send mails for each database.

这样做的问题是 php 脚本在发送 500 封电子邮件时被阻止在第 3 个数据库上,然后离开其他数据库等待轮到他们.

The problem with this is that the php script gets held up on, lets say, the 3rd database while sending 500 emails leaving the other databases after that one to wait for their turn.

我想弄清楚如何同时查询所有 24 个数据库并同时发送电子邮件队列.

I am trying to figure out how i can query all 24 databases at the same time and send the email queue at the same time.

有什么建议吗?

推荐答案

使用 curl_multi_open 可以做到这一点

On way to do this is with curl_multi_open

将你的脚本分成两部分,你可以让一个 php 文件(比如 email_out.php)使用 db 名称(或者一些用于查找 db 名称的变量,开关将在 for 循环中或在 email_out 中.php),然后根据那个脚本做群发邮件.

Split your script into two, you can make one php file (say email_out.php) take the db name (or some variable that's used to look up the db name, either the switch will be in the for loop or in email_out.php), and then do the mass email based of that one script.

第二部分使用 curl_multi_open 多次打开 email_out.php 脚本,有效地创建了到不同数据库的多个单独连接,这些脚本都可以在不同的时间解析,因为它们都是并行运行的.本质上,您的循环现在使用不同的参数多次将脚本添加到 curl_multi_open,然后异步执行所有这些.

the second part uses curl_multi_open to open the email_out.php script multiple times, effectively creating multiple separate connections to different db's, the scripts can all resolve at different times since they are all running in parallel. Essentially, your loop is now adding the script to curl_multi_open multiple times with different arguments and then executing all of them asynchronously.

class Fork
{
    private $_handles = array();
    private $_mh      = array();

    function __construct()
    {
        $this->_mh = curl_multi_init();
    }

    function add($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_multi_add_handle($this->_mh, $ch);
        $this->_handles[] = $ch;
        return $this;
    }

    function run()
    {
        $running=null;
        do {
            curl_multi_exec($this->_mh, $running);
            usleep (250000);
        } while ($running > 0);
        for($i=0; $i < count($this->_handles); $i++) {
            $out = curl_multi_getcontent($this->_handles[$i]);
            $data[$i] = json_decode($out);
            curl_multi_remove_handle($this->_mh, $this->_handles[$i]);
        }
        curl_multi_close($this->_mh);
        return $data;
    }
}

(来自 http:///gonzalo123.com/2010/10/11/speed-up-php-scripts-with-asynchronous-database-queries/)

所以你的循环看起来像这样:

So your loop would look something like this:

$fork = new Fork;
for ($i = 0; $i < 24; $i++) {
    $fork->add("email_out.php?t=" . $i);
}
$fork->run();

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

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