从另一个表中大MySQL数据库更新列(7个百万行)(Update column from anoth

2019-09-19 07:33发布

描述

我有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行。 所述part_code从塔details表示code从列parts表。 由于这些列是varchar ,我想将列添加id int(11)partspart_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)

所以,现在我有正确数据的ID列parts表。 加入后part_iddetails表:

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. 是更新查询OK,也可以以某种方式进行优化?
  2. 我会添加上的指标codeparts表,将查询在一个合理的时间运行,否则就会几天再次运行?
  3. 我怎样才能让一个(SQL /庆典/ PHP)的脚本,这样我可以看到查询执行的进展如何?

非常感谢你!

Answer 1:

正如我在问题中提到,我忘了就丢弃的索引parts表,所以我加了他们:

alter table parts add key code (code);

通过Puggan硒的回答启发,我试图用LIMITUPDATE在PHP脚本,但LIMIT不能与使用UPDATEJOIN在MySQL。 为了限制我添加了一个新列的查询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分钟:)谢谢大家!

PS:这里仍然是一个错误,因为我发现只剩下后4999行part_id = 0 ,但我做了人工了。



Answer 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”上面sugesten,你可以SE 5000行需要多长时间来处理

  3. 环以上查询

     while(TRUE) { $result = mysql_query($query); if(!$result) die('Failed: ' . mysql_error()); if(mysql_affected_rows() == 0) die('Done'); echo '.'; } 

编辑1次重写查询并限制错误的连接

您可以使用子查询,以避免多个表的更新:

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


Answer 3:

你可以尝试删除索引形成你想更新表。 MySQL的再现每行更新的索引。 这不会是为7M记录速度极快。



文章来源: Update column from another table in large mysql db (7 million rows)