您的位置:首页 > 其它

将单表600万的数据拆分到20张表的实现方法分享

2016-10-13 13:29 441 查看
将之前的单表数据600万按照用户id取模20的方式,拆分到20张表,平均每张表大概30万的数据,

先写脚本将线上的老数据读取写入到20张表,记录最大的表的id,从昨天的下班的6点开始跑一直跑到第二天的凌晨4点多数据跑完

今天上午10点20上代码测试,没问题了,再把最大的表的id到现在的数据再跑一次,保证数据没有丢失是最新的。

最底层的方法:调用: $table_name_str = Util_Tool::get_table_name('shop_user_score_logs', $this->uid, 20);传一个表的前缀,用户id,20,会返回对应的表名

public static function get_table_name($table_prefix,$lender_id,$mod){

     

     if(empty($table_prefix) || empty($lender_id) || empty($mod)){

         return FALSE;

     }

     $table_prefix = trim($table_prefix);

     $lender_id = intval($lender_id);

     $mod = intval($mod);

     

     $table_name = $table_prefix.'_'.$lender_id%$mod;

     return $table_name;

     

 }

后台数据统计的,for循环统计

      $sum_score_int = 0;

            for($i=0;$i<20;$i++){

              $table_name = 'shop_user_score_logs_'.$i;
$sql = "select
sum(score) as sum_score
    from
$table_name
   where
day between '$start_date' and '$end_date'
and state = '$state' and action='lottery'
";
$res = $this->db->execute($sql);   

                if($res){

                    $sum_score_int+=intval($res[0]['sum_score']);

                }

            }

            return  $execute[0]['sum_score'] = $sum_score_int;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: