公司在做游戏服务器合并的时候,对大批量数据表做了合并操作,难免会出现数据格式不一致问题。根据玩家反映BUG排查,是因为某个模块下日志表出现了数据格式问题导致。
目前想到的是有两种方案解决,第一种就是把所有的日志表数据修复;第二种就是把程序逻辑修改一下,保证查找格式正确。
我的做法是想把所有数据修复一下,就从线上数据库拿了7000条数据在本地修改测试,保证无误再执行下一步计划。
由于想到数据量会很大,我这里也顺便实验了一下多进程效率和单进程效率的对比。下面看下代码,有很多需要优化的地方....这里只是简单的案例.... 两个数据表在最下面会分享链接测试.....
多进程实例
<?php
//index.php
$start_memory = memory_get_usage(); //开始内存 echo ‘开始内存:‘ . $start_memory . "rn"; $start_time = microtime(true); //获取程序开始执行的时间 $mysqli = new mysqli();//实例化mysqli $mysqli->connect(‘localhost‘,‘root‘,‘wt000000‘,‘up‘); $all_uid_sql = ‘select uid from zc_kapai_fashion_show_intensify_log group by uid‘; $all_user_result = $mysqli->query($all_uid_sql); while (!!$_row = $all_user_result->fetch_assoc()) { $all_user[] = $_row[‘uid‘]; } //引入文件 require ‘up_sql.php‘; //count($all_user) = 84 $x_count = ceil(count($all_user)/10); //fork 9个进程 进程太多,可能会导致服务器系统资源耗尽而崩溃,所以必须控制进程数量 for($i = 0; $i < $x_count; $i) { $pids[$i] = pcntl_fork(); if($pids[$i] == -1) { die(‘fork error‘); } else if ($pids[$i]) { //父进程逻辑 pcntl_wait($status, WNOHANG); } else { //子进程逻辑 $start = $i * 10; $tmp_users = array_splice($all_user, $start, 10); if (is_array($tmp_users) && !empty($tmp_users)) { up_sql($tmp_users) ; } exit; } } $all_user_result->free(); $mysqli->close(); //别忘了关闭你的"小资源"; $end_time = microtime(true); //获取程序执行结束的时间 $run_time = ($end_time - $start_time) * 1000; //计算差值 毫秒 echo "[页面执行时间:{$run_time}]毫秒" . "rn"; $end_memory = memory_get_usage(); echo ‘运行后内存:‘. $end_memory . "rn"; echo ‘使用的内存:‘ . ($end_memory - $start_memory) . "rn"; echo ‘回到正常内存:‘.memory_get_usage();
<?php //up_sql.php //修改语句的方法 function up_sql ($temp_users = []) { $mysqli = new mysqli();//实例化mysqli $mysqli->connect(‘localhost‘,‘root‘,‘wt000000‘,‘up‘); foreach ($temp_users as $u) { $sql1 = ‘select id,fid from zc_kapai_fashion_show_intensify_log where uid =‘ .$u;//创建一句SQL语句 $sql2 = ‘select id from zc_kapai_own_fashion_show where uid =‘ . $u;//创建一句SQL语句 $result1=$mysqli->query($sql1); $result2=$mysqli->query($sql2); $log = []; $show = []; while (!!$_row = $result1->fetch_assoc()) { $log[] = $_row; } while (!!$_row = $result2->fetch_assoc()) { $show[] = $_row; } $test = $show[0][‘id‘]; $return_arr = []; foreach ($log as $lv) { foreach ($show as $sv) { $num = intval(substr((string)$sv[‘id‘], 12, 4)); //show id ,拿来对比log的fid if ($num == $lv[‘fid‘] ) { $arr = []; $log_len = strlen((string)$lv[‘fid‘]); $cha = 16 - $log_len; if (intval($cha) > 0) { $tou = (string)(substr($test, 0, $cha)); $val = $tou. (string)$num; } $arr[‘id‘] = $lv[‘id‘]; $arr[‘to_val‘] = $val; $return_arr[] = $arr; } } } foreach ($return_arr as $v) { $sql = ‘update zc_kapai_fashion_show_intensify_log set fid = ‘ . $v[‘to_val‘] . ‘ where id =‘ .$v[‘id‘]; $mysqli->query($sql); } } $result1->free();//释放查询内存(销毁) $result2->free();//释放查询内存(销毁) $mysqli->close();//别忘了关闭你的"小资源"; }
Linux下Cli模式运行index.php,查看结果
# php index.php 开始内存:385520 [页面执行时间:160.7940196991]毫秒 运行后内存:399096 使用的内存:13576 回到正常内存:399096
单进程实例
<?php //index.php $start_memory = memory_get_usage(); //开始内存 echo ‘开始内存:‘ . $start_memory . "rn"; $start_time = microtime(true); //获取程序开始执行的时间 $mysqli=new mysqli();//实例化mysqli $mysqli->connect(‘localhost‘,‘root‘,‘wt000000‘,‘up‘); $all_uid_sql = ‘select uid from zc_kapai_fashion_show_intensify_log group by uid‘; $all_user_result =$mysqli->query($all_uid_sql); while (!!$_row = $all_user_result->fetch_assoc()) { $all_user[] = $_row[‘uid‘]; } foreach ($all_user as $u) { $sql1=‘select id,fid from zc_kapai_fashion_show_intensify_log where uid =‘ .$u; $sql2=‘select id from zc_kapai_own_fashion_show where uid =‘ . $u; $result1=$mysqli->query($sql1);//执行sql语句把结果集赋给$result $result2=$mysqli->query($sql2);//执行sql语句把结果集赋给$result $log = []; $show = []; while (!!$_row = $result1->fetch_assoc()) { $log[] = $_row; } while (!!$_row = $result2->fetch_assoc()) { $show[] = $_row; } $test = $show[0][‘id‘]; $return_arr = []; foreach ($log as $lv) { foreach ($show as $sv) { $num = intval(substr((string)$sv[‘id‘], 12, 4)); //show id ,拿来对比log的fid if ($num == $lv[‘fid‘] ) { $arr = []; $log_len = strlen((string)$lv[‘fid‘]); $cha = 16 - $log_len; if (intval($cha) > 0) { $tou = (string)(substr($test, 0, $cha)); $val = $tou. (string)$num; } $arr[‘id‘] = $lv[‘id‘]; $arr[‘to_val‘] = $val; $return_arr[] = $arr; } } } foreach ($return_arr as $v) { $sql = ‘update zc_kapai_fashion_show_intensify_log set fid = ‘ . $v[‘to_val‘] . ‘ where id =‘ .$v[‘id‘]; $mysqli->query($sql); } } $result1->free(); //释放查询内存(销毁) $result2->free(); //释放查询内存(销毁) $all_user_result->free(); $mysqli->close(); //别忘了关闭你的"小资源"; $end_time = microtime(true); //获取程序执行结束的时间 $run_time = ($end_time - $start_time) * 1000; //计算差值 毫秒 echo "[页面执行时间:{$run_time}]毫秒" . "rn"; $end_memory = memory_get_usage(); echo ‘运行后内存:‘. $end_memory . "rn"; echo ‘使用的内存:‘ . ($end_memory - $start_memory) . "rn"; echo ‘回到正常内存:‘.memory_get_usage();
Linux下Cli模式运行index.php,查看结果
# php index.php 开始内存:398768 [页面执行时间:9905.3988456726]毫秒 运行后内存:423736 使用的内存:24968 回到正常内存:423736
测试结果明显对比,内存和执行时间....这里只是简单测试。
两个数据表