如何加快 MySQL 插入速度?


藉助以下語法,同時插入多條記錄時,可以加速 MySQL 插入速度

START TRANSACTION
insert into insertDemo(yourColumnName1,yourColumnName2,...N) values(yourValue1,yourValue2,....N),(yourValue1,yourValue2,....N),.......N
commit

我們首先建立一個演示表

mysql> create table insertDemo
   -> (
   -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentName varchar(20),
   -> StudentAge int
   -> );
Query OK, 0 rows affected (0.72 sec)

同時插入多條記錄。查詢如下 −

mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into insertDemo(StudentName,StudentAge) values('John',21),('Carol',22),('Bob',21),('David',24),
   -> ('Maxwell',25),('Mike',22);
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> commit;
Query OK, 0 rows affected (0.14 sec

使用 select 語句顯示錶中的所有記錄。查詢如下 −

mysql> select *from insertDemo;

輸出結果如下

+-----------+-------------+------------+
| StudentId | StudentName | StudentAge |
+-----------+-------------+------------+
|         1 | John        |         21 |
|         2 | Carol       |         22 |
|         3 | Bob         |         21 |
|         4 | David       |         24 |
|         5 | Maxwell     |         25 |
|         6 | Mike        |         22 |
+-----------+-------------+------------+
6 rows in set (0.00 sec)

更新於: 30-Jul-2019

103 人圍觀

開始您的 職業生涯

完成課程取得認證

開始
廣告