【发布时间】: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)
我的问题是:
- 更新查询是否正常或者可以通过某种方式进行优化?
- 我将在
parts表的code列上添加索引,查询会在合理的时间内运行,还是会再次运行几天? - 如何制作 (sql/bash/php) 脚本以便查看查询执行的进度?
非常感谢!
【问题讨论】:
-
问题出在partcode这里是varchar,所以执行需要很多时间\
-
我知道问题所在。这就是我尝试添加 int 列的原因。