Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

permintaan lambat mysql

Berikut ini cukup cepat, membutuhkan waktu sedikit lebih dari 6 menit dengan 10 juta baris tetapi tabel contoh memiliki lebih sedikit bidang dan indeks daripada tabel produksi Anda, jadi perkirakan ini akan memakan waktu lebih lama jika Anda memutuskan untuk menggunakannya!

Catatan:contoh dilakukan pada OS windows sehingga Anda harus mengubah nama path dan \r\n menjadi \n agar sesuai dengan standar linux !

Ini tabel saya yang sudah ada (mesin InnoDB):

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)

Buat tabel versi baru yang menyertakan bidang baru yang Anda perlukan:

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;

Temukan lokasi folder Unggahan Anda:

select @@secure_file_priv;

Ekspor data dalam urutan PK dari tabel pelanggan lama ke dalam format csv:

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)

Muat file customer.dat ke tabel pelanggan baru:

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)

Konfirmasi bahwa tabel baru terlihat baik-baik saja:

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)

Lepaskan tabel lama dan ganti nama baru:

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)

Itu saja!



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Hubungkan Java ke database MySQL

  2. MySQL Buat Tabel sebagai SELECT

  3. mysql5.58 unstart server di mac os 10.6.5

  4. MySQL setara dengan fungsi DECODE di Oracle

  5. Cara mengupload banyak gambar di laravel