Reputation: 369
what is the best way to insert a lot of data to mysql database?
Upvotes: 1
Views: 1191
Reputation: 1270993
You are not just doing an insert, but also using on duplicate key update
.
I would suggest that you take a bulk-load approach:
load data infile
into a staging table.insert . . . on duplicate key update
on the staging table.The final step looks similar to your query:
INSERT INTO players (name, online, world, level, vocation, month, week, today)
SELECT name, online, world, level, vocation, time, time, time
FROM players_staging
ON DUPLICATE KEY UPDATE
online = values(online), world = values(world), level = values(level),
vocation = values(vocation), month = month + values(time),
week = week + values(time), today = today + values(time);
Bulk operations are faster for two reasons. First, they requires fewer calls into the databases -- setting up queries, returning results, and other overhead going back and forth. Second, they reduce the amount of logging and locking required for the operations.
Upvotes: 1