Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从大型mysql数据库中的另一个表更新列(700万行) 描述_Mysql_Large Data Volumes - Fatal编程技术网

从大型mysql数据库中的另一个表更新列(700万行) 描述

从大型mysql数据库中的另一个表更新列(700万行) 描述,mysql,large-data-volumes,Mysql,Large Data Volumes,我有两个具有以下结构的表(删除了不相关的列): 及 表parts包含184147行,details包含7278870行。 details中的part\u code列表示parts表中的code列。 由于这些列是varchar,我想将id int(11)列添加到parts,将part\u id int(11)列添加到details。我试过这个: mysql> alter table parts drop primary key; Query OK, 184147 rows affected

我有两个具有以下结构的表(删除了不相关的列):

parts
包含184147行,
details
包含7278870行。
details
中的
part\u code
列表示
parts
表中的
code
列。 由于这些列是
varchar
,我想将
id int(11)
列添加到
parts
,将
part\u id int(11)
列添加到
details
。我试过这个:

mysql> alter table parts drop primary key;
Query OK, 184147 rows affected (0.66 sec)
Records: 184147  Duplicates: 0  Warnings: 0

mysql> alter table parts add column
       id int(11) not null auto_increment primary key first;
Query OK, 184147 rows affected (0.55 sec)
Records: 184147  Duplicates: 0  Warnings: 0

mysql> select id, code from parts limit 5;
+----+-------------------------+
| id | code                    |
+----+-------------------------+
|  1 | Yhk0KqSMeLcfH1KEfykihQ2 |
|  2 | IMl4iweZdmrBGvSUCtMCJA2 |
|  3 | rAKZUDj1WOnbkX_8S8mNbw2 |
|  4 | rV09rJ3X33-MPiNRcPTAwA2 |
|  5 | LPyIa_M_TOZ8655u1Ls5mA2 |
+----+-------------------------+
5 rows in set (0.00 sec)
因此,现在我在
parts
表中有了id列和正确的数据。将
part\u id
列添加到
details
表后:

mysql> alter table details add column part_id int(11) not null after part_code;
Query OK, 7278870 rows affected (1 min 17.74 sec)
Records: 7278870  Duplicates: 0  Warnings: 0
# drop the primary key,
alter table details drop primary key;
# so I can create an auto_increment column
alter table details add id int not null auto_increment primary key;
# alter the id column and remove the auto_increment
alter table details change id id int not null;
# drop again the primary key
alter table details drop primary key;
# add new indexes
alter table details add primary key ( id, sku, num, part_code );
现在最大的问题是如何相应地更新
part\u id
?以下查询:

mysql> update details d
       join parts p on d.part_code = p.code
       set d.part_id = p.id;
一直跑了30个小时直到我把它弄死

请注意,这两个表都是MyISAM:

mysql> select engine from information_schema.tables where table_schema = 'db_name' and (table_name = 'parts' or table_name = 'details');
+--------+
| ENGINE |
+--------+
| MyISAM |
| MyISAM |
+--------+
2 rows in set (0.01 sec)
我刚刚意识到其中一个问题是,在
parts
表上删除键时,我删除了
code
列上的索引。另一方面,我在
details
表上有以下索引(省略了一些不相关的列):

我的问题是:
  • 更新查询是正常的还是可以以某种方式进行优化
  • 我将在
    parts
    表的
    code
    列中添加索引,查询将在合理的时间内运行,还是将再次运行数天
  • 如何生成(sql/bash/php)脚本,以便查看查询执行的进度
  • 多谢各位

  • 您可能需要添加一个where和一个limit,以便可以分块更新它

    update details d
    join parts p on d.part_code = p.code
    set d.part_id = p.id
    WHERE d.part_id =0
    LIMIT 5000;
    
  • 使用索引会快得多,如果您像上面“1”中的sugesten那样执行一个查询,您可以确定处理5000行所需的时间

  • 循环上面的查询

    while(TRUE)
    {
        $result = mysql_query($query);
        if(!$result) die('Failed: ' . mysql_error());
        if(mysql_affected_rows() == 0) die('Done');
        echo '.';
    }
    
  • 编辑1 重写查询do以限制联接上的错误

    您可以使用子查询来避免多表更新:

    UPDATE details
    SET part_id = (SELECT id FROM parts WHERE parts.code = details.part_code)
    WHERE part_id = 0
    LIMIT 5000;
    

    您可以尝试从要更新的表中删除索引。MySQL在每次行更新时重新创建索引。700万条记录的速度不会很快。

    正如我在问题中提到的,我忘记了在
    部分
    表上删除的索引,所以我添加了它们:

    alter table parts add key code (code);
    
    受Puggan Se回答的启发,我尝试在PHP脚本中对
    UPDATE
    使用
    LIMIT
    ,但是
    LIMIT
    不能与MySQL中的
    UPDATE
    JOIN
    一起使用。为了限制查询,我在
    details
    表中添加了一个新列:

    mysql> alter table details add column part_id int(11) not null after part_code;
    Query OK, 7278870 rows affected (1 min 17.74 sec)
    Records: 7278870  Duplicates: 0  Warnings: 0
    
    # drop the primary key,
    alter table details drop primary key;
    # so I can create an auto_increment column
    alter table details add id int not null auto_increment primary key;
    # alter the id column and remove the auto_increment
    alter table details change id id int not null;
    # drop again the primary key
    alter table details drop primary key;
    # add new indexes
    alter table details add primary key ( id, sku, num, part_code );
    
    现在我可以使用“限制”:

    下面是完整的PHP脚本:

    $started = time();
    $i = 0;
    $total = 7278870;
    
    echo "Started at " . date('H:i:s', $started) . PHP_EOL;
    
    function timef($s){
        $h = round($s / 3600);
        $h = str_pad($h, 2, '0', STR_PAD_LEFT);
        $s = $s % 3600;
        $m = round( $s / 60);
        $m = str_pad($m, 2, '0', STR_PAD_LEFT);
        $s = $s % 60;
        $s = str_pad($s, 2, '0', STR_PAD_LEFT);
        return "$h:$m:$s";
    }
    
    while (1){
        $i++;
        $j = $i * 5000;
        $k = $j + 4999;
        $result = mysql_query("
            update details d
            join parts p on d.part_code = p.code
            set d.part_id = p.id
            where d.id between $j and $k
        ");
        if(!$result) die(mysql_error());
        if(mysql_affected_rows() == 0) die(PHP_EOL . 'Done!');
        $p = round(($i * 5000) / $total, 4) * 100;
        $s = time() - $started;
        $ela = timef($s);
        $eta = timef( (( $s / $p ) * 100) - $s );
        $eq = floor($p/10);
        $show_gt = ($p == 100);
        $spaces = $show_gt ? 9 - $eq : 10 - $eq;
        echo "\r {$p}% | [" . str_repeat('=', $eq) . ( $show_gt ? '' : '>' ) . str_repeat(' ', $spaces) . "] | Elapsed: ${ela} | ETA: ${eta}";
    }
    
    下面是一个屏幕截图:

    如您所见,整个过程只花了不到5分钟:) 谢谢大家!


    注意:仍然有一个错误,因为我后来发现4999行剩余
    part\u id=0
    ,但我已经手动执行了。

    问题在于partcode,它是一个varchar,因此执行需要很多时间\n我知道问题所在。这就是我试图添加int列的原因。它失败了<代码>限制不能与
    更新
    一起使用。看你的回答激励了我。在看到编辑之前,我自己解决了这个问题。现在我将发布我的解决方案。Mysql不会为我不更新的列重新创建索引,但感谢您的回答就像您的终端颜色一样,让我想起以前显示器只有一种颜色:-)
    update details d
    join parts p on d.part_code = p.code
    set d.part_id = p.id
    where d.id between 1 and 5000;
    
    $started = time();
    $i = 0;
    $total = 7278870;
    
    echo "Started at " . date('H:i:s', $started) . PHP_EOL;
    
    function timef($s){
        $h = round($s / 3600);
        $h = str_pad($h, 2, '0', STR_PAD_LEFT);
        $s = $s % 3600;
        $m = round( $s / 60);
        $m = str_pad($m, 2, '0', STR_PAD_LEFT);
        $s = $s % 60;
        $s = str_pad($s, 2, '0', STR_PAD_LEFT);
        return "$h:$m:$s";
    }
    
    while (1){
        $i++;
        $j = $i * 5000;
        $k = $j + 4999;
        $result = mysql_query("
            update details d
            join parts p on d.part_code = p.code
            set d.part_id = p.id
            where d.id between $j and $k
        ");
        if(!$result) die(mysql_error());
        if(mysql_affected_rows() == 0) die(PHP_EOL . 'Done!');
        $p = round(($i * 5000) / $total, 4) * 100;
        $s = time() - $started;
        $ela = timef($s);
        $eta = timef( (( $s / $p ) * 100) - $s );
        $eq = floor($p/10);
        $show_gt = ($p == 100);
        $spaces = $show_gt ? 9 - $eq : 10 - $eq;
        echo "\r {$p}% | [" . str_repeat('=', $eq) . ( $show_gt ? '' : '>' ) . str_repeat(' ', $spaces) . "] | Elapsed: ${ela} | ETA: ${eta}";
    }