3

描述

我有 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_codedetails表示表中的codeparts。由于这些列是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)

所以现在我在表中有正确数据的 id 列parts。将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. code我将在表中的列上添加索引parts,查询会在合理的时间内运行,还是会再次运行几天?
  3. 如何制作 (sql/bash/php) 脚本以便查看查询执行的进度?

非常感谢!

4

3 回答 3

5

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

alter table parts add key code (code);

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

# 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,但我已经手动完成了。

于 2012-07-13T00:41:44.827 回答
1
  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. whit index会快很多,如果你在上面的'1'中做一个查询,你可以确定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;
于 2012-07-11T10:24:16.000 回答
0

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

于 2012-07-11T14:55:47.633 回答