Aggiungere una nuova colonna a una tabella enorme con una ALTER TABLE può essere terribilmente lento. L'approccio qui sotto ricarica i dati attraverso un file CSV e, su una tabella InnoDB da 10 milioni di righe, è terminato in poco più di sei minuti. La tua tabella di produzione ha più campi e indici, quindi aspettati tempi più lunghi.
Nota: questo esempio è stato eseguito su Windows, quindi adatta i percorsi e converti \r\n in \n per Linux.
La tabella esistente
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) Crea la nuova tabella con il campo aggiuntivo
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; Trova la cartella degli upload
select @@secure_file_priv; Esporta i vecchi dati in ordine di chiave primaria
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) Caricali nella nuova tabella
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 -- rappresenta il nuovo campo split ) set name = nullif(name,''); commit; Query OK, 10000000 rows affected (6 min 0.14 sec) Verifica il risultato
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) Scambia le tabelle
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) E questo è l'intero processo: esporta, ricarica, verifica e scambia.