I am trying to insert many users into a MySQL database with two tables:
The first table contains user data. An INSERT example is as follows ( id is the primary key, mail is the unique key):
INSERT INTO users (id, mail, name) VALUES (NULL, " foo@bar.tld ", "John Smith") ON DUPLICATE KEY UPDATE name = VALUE(name)
The second table contains the group to which the user belongs. It stores only two foreign keys users_id and groups_id . An example request is as follows:
INSERT INTO users_groups (users_id, groups_id) VALUES (LAST_INSERT_ID(), 1)
This setting works great for small datasets. When I import large amounts of data (> 1M rows), INSERT slows down. Obviously, it would be much better to do a batch insert:
INSERT INTO users (id, mail, name) VALUES (NULL, " foo@bar.tld ", "John Smith"), (NULL, " baz@qux.tld ", "Anna Smith") ON DUPLICATE KEY UPDATE name = VALUE(name)
and
INSERT INTO users_groups (users_id, groups_id) VALUES (LAST_INSERT_ID(), 1), (LAST_INSERT_ID(), 4)
Of course, the problem is that LAST_INSERT_ID() returns only one (first) packet identifier INSERT .
So, I will need a βnestedβ INSERT package that IMO does not exist in MySQL.
What can be done to make INSERT faster?