MySQL 如何加速插入
当您同时插入多个记录时,可以使用以下语法来加速MySQL插入
START TRANSACTION
insert into insertDemo(您的列名1,您的列名2,...N) values(您的值1,您的值2,....N),(您的值1,您的值2,....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)
阅读更多:MySQL 教程
极客教程