ALTER TABLE customers ADD split INT(1);10密耳。记录......我执行了这个命令1小时仍在加载..有什么方法可以让它更快完成?
答案 0 :(得分:6)
以下内容非常快,耗时超过6分钟,行数为1000万行,但示例表的字段和索引比生产表少,所以如果您决定使用它,预计会花费更长的时间!
注意:该示例是在Windows操作系统上完成的,因此您必须将路径名和\ r \ n更改为\ n以符合Linux标准!
这是我现有的表格(innodb引擎ofc):
drop table if exists customers;
create table customers
(
customer_id int unsigned not null auto_increment primary key,
name varchar(255) not null,
country_id tinyint unsigned not null default 0,
key (country_id)
)
engine=innodb;
mysql> select count(*) from customers;
+----------+
| count(*) |
+----------+
| 10000000 |
+----------+
1 row in set (1.78 sec)
创建新版本的表格,其中包含您需要的新字段:
drop table if exists customers_new;
create table customers_new
(
customer_id int unsigned not null auto_increment primary key,
name varchar(255) not null,
country_id tinyint unsigned not null default 0,
split tinyint not null default 0,
key (country_id)
)
engine=innodb;
将旧客户表中PK数据的数据导出为csv格式:
select * into outfile 'c:\\customers.dat'
fields terminated by '|' optionally enclosed by '"'
lines terminated by '\r\n'
from customers order by customer_id;
Query OK, 10000000 rows affected (17.39 sec)
将customer.dat文件加载到新的客户表中:
truncate table customers_new;
set autocommit = 0;
load data infile 'c:\\customers.dat'
into table customers_new
fields terminated by '|' optionally enclosed by '"'
lines terminated by '\r\n'
(
customer_id,
name,
country_id,
@dummy -- represents the new split field
)
set
name = nullif(name,'');
commit;
Query OK, 10000000 rows affected (6 min 0.14 sec)
检查一切是否正常
select * from customers_new order by customer_id desc limit 1;
+-------------+-------------------+------------+-------+
| customer_id | name | country_id | split |
+-------------+-------------------+------------+-------+
| 10000000 | customer 10000000 | 218 | 0 |
+-------------+-------------------+------------+-------+
1 row in set (0.00 sec)
insert into customers_new (name, country_id, split) values ('f00',1,1);
Query OK, 1 row affected (0.07 sec)
select * from customers_new order by customer_id desc limit 1;
+-------------+------+------------+-------+
| customer_id | name | country_id | split |
+-------------+------+------------+-------+
| 10000001 | f00 | 1 | 1 |
+-------------+------+------------+-------+
1 row in set (0.00 sec)
删除旧表并重命名新表:
drop table customers;
Query OK, 0 rows affected (0.18 sec)
rename table customers_new to customers;
Query OK, 0 rows affected (0.05 sec)
select * from customers order by customer_id desc limit 1;
+-------------+------+------------+-------+
| customer_id | name | country_id | split |
+-------------+------+------------+-------+
| 10000001 | f00 | 1 | 1 |
+-------------+------+------------+-------+
1 row in set (0.00 sec)
这就是所有人!
答案 1 :(得分:1)
可能不是代码..
关闭所有其他应用程序,确保没有其他任何东西占用您的CPU使用率,没有恶意软件?获得更快的计算机?
如果您能告诉我们您正在使用的环境设置等,这将有所帮助。它可能涉及您的网络,服务器等许多事情。