描述
我有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)
以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)
我的问题是:
- 是更新查询OK,也可以以某种方式进行优化?
- 我会添加上的指标
code
列parts
表,将查询在一个合理的时间运行,否则就会几天再次运行? - 我怎样才能让一个(SQL /庆典/ PHP)的脚本,这样我可以看到查询执行的进展如何?
非常感谢你!