What is the fastest way to insert a large number of rows into a MySQL table?

MySQLMySQLi Database

The syntax for the fastest way is given below. Here, we have used INSERT INTO just once and formed an optimized way −

insert into yourTableName values(NULL,yourValue1',yourValue2),(NULL,yourValue1',yourValue2),....N;

Let us first create a table −

mysql> create table DemoTable1839
     (
     ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
     ClientName varchar(20),
     ClientAge int
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1839 values(NULL,'Chris',29),(NULL,'Chris',29),(NULL,'Chris',29),(NULL,'Chris',29),(NULL,'Chris',29),(NULL,'Chris',29),(NULL,'Chris',29),(NULL,'Chris',29);
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0

Display all records from the table using select statement −

mysql> select * from DemoTable1839;

This will produce the following output −

+----------+------------+-----------+
| ClientId | ClientName | ClientAge |
+----------+------------+-----------+
|        1 | Chris      |        29 |
|        2 | Chris      |        29 |
|        3 | Chris      |        29 |
|        4 | Chris      |        29 |
|        5 | Chris      |        29 |
|        6 | Chris      |        29 |
|        7 | Chris      |        29 |
|        8 | Chris      |        29 |
+----------+------------+-----------+
8 rows in set (0.00 sec)
raja
Published on 26-Dec-2019 10:27:54
Advertisements