以下是非常快的,需要花费超过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)
Run Code Online (Sandbox Code Playgroud)
创建表的新版本,其中包含您需要的新字段:
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;
Run Code Online (Sandbox Code Playgroud)
将旧客户表中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)
Run Code Online (Sandbox Code Playgroud)
将customer.dat文件加载到新的customer表中:
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)
Run Code Online (Sandbox Code Playgroud)
检查一切是否正常
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)
Run Code Online (Sandbox Code Playgroud)
删除旧表并重命名新表:
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)
Run Code Online (Sandbox Code Playgroud)
这就是所有人!
| 归档时间: |
|
| 查看次数: |
2030 次 |
| 最近记录: |