Het volgende is vrij snel, duurt iets meer dan 6 minuten met 10 miljoen rijen, maar de voorbeeldtabel heeft minder velden en indexen dan uw productietabel, dus verwacht dat het in uw geval iets langer zal duren als u besluit om het te gebruiken!
Let op:het voorbeeld is gemaakt op Windows OS, dus je zult padnamen en \r\n in \n moeten veranderen om te voldoen aan de linux-standaarden!
Hier is mijn bestaande tabel (InnoDB-engine):
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)
Maak een nieuwe versie van de tabel die het nieuwe veld bevat dat u nodig heeft:
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;
Zoek de locatie van uw map Uploads:
select @@secure_file_priv;
Exporteer de gegevens in PK-volgorde van de oude klantentabel naar csv-indeling:
select * into outfile 'C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\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)
Laad het bestand customer.dat in de nieuwe klantentabel:
truncate table customers_new;
set autocommit = 0;
load data infile 'C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\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)
Bevestig dat de nieuwe tabel er goed uitziet:
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)
Laat de oude tabel vallen en hernoem de nieuwe:
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)
Dat is alles mensen!