sql >> Database teknologi >  >> RDS >> Mysql

mysql langsom forespørgsel

Det følgende er ret hurtigt, tager lidt over 6 minutter med 10 millioner rækker, men eksempeltabellen har færre felter og indekser end din produktionstabel, så forvent, at det tager lidt længere tid i dit tilfælde, hvis du beslutter dig for at bruge det!

Bemærk:eksemplet blev lavet på Windows OS, så du bliver nødt til at ændre stinavne og \r\n til \n for at overholde linux-standarderne!

Her er min eksisterende tabel (InnoDB-motor):

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)

Opret en ny version af tabellen, som indeholder det nye felt, du har brug for:

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;

Find placeringen af ​​din upload-mappe:

select @@secure_file_priv;

Eksporter dataene i PK-rækkefølge fra den gamle kundetabel til csv-format:

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)

Indlæs filen customer.dat i den nye kundetabel:

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)

Bekræft, at den nye tabel ser ok ud:

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)

Slip den gamle tabel og omdøb den nye:

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)

Det er det hele folkens!



  1. Hvordan kan jeg omdøbe kolonne i laravel ved hjælp af migration?

  2. Sådan opgraderes MariaDB 5.5 til MariaDB 10.1 på CentOS/RHEL 7 og Debian Systems

  3. LIKE vs CONTAINS på SQL Server

  4. Hvordan betragter NULL som MAX-datoen i stedet for at ignorere den i MySQL?