设为首页 加入收藏

TOP

合并不同服务器的数据表(thinkphp5+mysql)(二)
2018-10-23 18:08:45 】 浏览:459
Tags:合并 不同 服务器 数据 thinkphp5 mysql
sn['channel_id']; $channel_sql = " ALTER TABLE `$channel_table` ADD COLUMN `channel_id` int(10) UNSIGNED NOT NULL DEFAULT $channel_id AFTER `id`"; $channel_result = Db::execute($channel_sql); if ($channel_result != false) { $msg = "$key 渠道的$this->member_table" . '表 的渠道ID字段添加成功'; echo $msg; Log::record($msg, 'INFO'); //添加成功后可以删除多余的映射表 Db::execute('DROP TABLE ' . $fed_channel_table); } else { $msg = "$key 渠道的$this->member_table" . '表 的渠道ID字段添加失败'; echo $msg; Log::record($msg, 'INFO'); exit(); } } $keys = array_keys($this->dsns); $merge_sql .= ' select distinct * from ' . $channel_table . ($key != end($keys) ' union all ' : ''); } Log::record($merge_sql, 'INFO'); //不渠道的表合并成 $result = Db::execute($merge_sql); if ($result == false) { echo $msg = $this->member_table . '合并表创建失败!'; Log::record($msg, 'INFO'); } else { echo $msg = $this->member_table . '合并表创建成功!'; Log::record($msg, 'INFO'); } }

上面大概的思路是通过show create table 把需要合并的表的创建mysql 获取,然后把引擎改成FEDERATED,然后在本地再次创建一份视图或者表,因为我这里需要新增一个channel_id字段到其它服务器映射到本地的表中,但是FEDERATED引擎不能操作表,所以需要在多一步步骤;

表创建过后,然后通过mysql 的 union 或者 union all 合并成一张表,到此其它服务器的表在本地数据库已合并在一起,有数据了接下来就好办了;

效果:

某个服务器的表

\

合并过后的表

\

3.同步数据
//同步会员表数据
    public function SynMemberTableData()
    {
        //把会员插入的信息另写一个日志
        Log::init([
            'type' => 'File',
            'path' => LOG_PATH . 'MemberLog' . DS,
        ]);
        $ts = microtime(true);
        Log::record("-----------------start写入会员信息---------执行时间-----------" . date("H:i:s"), 'INFO');
        Debug::remark('begin');


        //统计一共有多少用户

        $total = Db::table($this->member_table_merge)->group('mobile,channel_id')->count();

        Log::record('一共有' . $total . '个用户', 'INFO');

        if (!empty($total)) {
            //进程处理数据条数
            $limit = 10000;
            $pages = ceil($total / $limit);

            for ($i = 1; $i <= $pages; $i++) {
                $members = Db::table($this->member_table_merge)->group('mobile,channel_id')->order('channel_id')->page($i, $limit)->select();
                //多进程
                foreach ($members as $k => $member) {
                    //同个渠道的人是否已经有了
                    $has = $this->member_model->where(['mobile' => $member['mobile'], 'channel_id' => $member['channel_id']])->count();

                    if (empty($has)) {
                        unset($member['id']);
                        $insert_id = $this->member_model->insertGetId($member);
                        if ($insert_id) {
                            $msg = '用户:' . $member['username'] . '[channel_id=' . $member['channel_id'] . ']数据插入成功,插入ID为:' . $insert_id;
                            echo $msg;
                            Log::record($msg, 'INFO');
                        } else {
                            $msg = '用户:' . $member['username'] . '[channel_id=' . $member['channel_id'] . ']数据插入失败';
                            echo $msg;
                            Log::record($msg, 'Error');
                            continue;
                        }
                    } else {
                        continue;
                    }

                    unset($member[$k]);//unset()
                }


//
//                $reserveProcess = new \Swoole\Process(function ($worker) use ($i, $limit, $member_model, $member_dis_model) {
//
//
//
//                }, false);
                Debug::remark('end');
                Log::record("-----------------会员信息写入end-------------结束时间-------" . date("H:i:s"), 'INFO');
                Log::record('写入耗时' . (microtime(true) - $ts) . 's ', 'debug');
                Log::record('所耗内存' . Debug::getRangeMem('begin', 'end') . 'kb', 'debug');
                Log::save();
                echo "end SynMemberTableData task..." . (microtime(true) - $ts) . "s " . "
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇mysql查询操作 下一篇存储过程中使用Exec拼接SQL语句造..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目