【问题标题】:Update column from another table in large mysql db (7 million rows)从大型 mysql 数据库中的另一个表更新列(700 万行)
【发布时间】:2012-07-10 22:27:34
【问题描述】:

说明

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

mysql> explain parts;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| code        | varchar(32)  | NO   | PRI | NULL    |       |
| slug        | varchar(255) | YES  |     | NULL    |       |
| title       | varchar(64)  | YES  |     | NULL    |       |
+-------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> explain details;
+-------------------+--------------+------+-----+---------+-------+
| Field             | Type         | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| sku               | varchar(32)  | NO   | PRI | NULL    |       |
| description       | varchar(700) | YES  |     | NULL    |       |
| part_code         | varchar(32)  | NO   | PRI |         |       |
+-------------------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

parts 包含 184147 行,details 包含 7278870 行。 details 中的 part_code 列代表 parts 表中的 code 列。 由于这些列是varchar,我想将id int(11) 列添加到parts,并将part_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_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

现在最大的问题是如何相应地更新part_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 表上有以下索引(省略了一些不相关的列):

mysql> show indexes from details;
+---------+------------+----------+--------------+-------------+-----------+-------------+------------+
| Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Index_type |
+---------+------------+----------+--------------+-------------+-----------+-------------+------------+
| details |          0 | PRIMARY  |            1 | sku         | A         |        NULL | BTREE      |
| details |          0 | PRIMARY  |            3 | part_code   | A         |     7278870 | BTREE      |
+---------+------------+----------+--------------+-------------+-----------+-------------+------------+
2 rows in set (0.00 sec)

我的问题是:

  1. 更新查询是否正常或者可以通过某种方式进行优化?
  2. 我将在parts 表的code 列上添加索引,查询会在合理的时间内运行,还是会再次运行几天?
  3. 如何制作 (sql/bash/php) 脚本以便查看查询执行的进度?

非常感谢!

【问题讨论】:

  • 问题出在partcode这里是varchar,所以执行需要很多时间\
  • 我知道问题所在。这就是我尝试添加 int 列的原因。

标签: mysql large-data-volumes


【解决方案1】:

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

alter table parts add key code (code);

受 Puggan Se 回答的启发,我尝试在 PHP 脚本中在 UPDATE 上使用 LIMIT,但在 MySQL 中,LIMIT 不能与 UPDATEJOIN 一起使用。为了限制查询,我在details 表中添加了一个新列:

# 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 );

现在我可以使用“限制”了:

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;

这是完整的 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 分钟 :) 谢谢大家!

P.S.:还有一个错误,因为我后来发现 part_id = 0 还剩下 4999 行,但我已经手动完成了。

【讨论】:

  • 喜欢你的终端颜色,让我想起以前显示器只有一种颜色的日子:-)
【解决方案2】:
  1. 您可能想要添加位置和限制,以便您可以分块更新它

    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;
    
  2. 白色索引会快很多,如果您按照上面“1”中的建议执行一个查询,您可以确定处理 5000 行需要多长时间

  3. 在查询上方循环

    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;

【讨论】:

  • 它失败了。 LIMIT 不能与 UPDATE 一起使用。见stackoverflow.com/a/4291851/539153
  • 你的回答启发了我。在看到编辑之前我自己解决了。我现在将发布我的解决方案。
【解决方案3】:

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

【讨论】:

  • Mysql 不会为我没有更新的列重新创建索引,但是谢谢你的回答
猜你喜欢
  • 2013-04-29
  • 2014-11-16
  • 2014-09-04
  • 1970-01-01
  • 2019-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多