1

what is the best way to insert a lot of data to mysql database?

7
  • 2
    Please, have a look at BULK INSERT. Commented Jan 19, 2016 at 12:20
  • Hi , you can make a stringbuild and execute it one time, because you have too much IO, for example , "insert ..... ;" + "insert ......" to one string Commented Jan 19, 2016 at 12:22
  • I think I looked at some examples about strinbuild last night. is that when I loop through all my Players, and make a gigantic query, and then some append at the end and close it off with semi-colons? If so, then I had a lot of trouble with it. Because I couldn't insert my variables like @level, they are assigned later on at the bottom as you can see in my code. So I dont know how to do this. I find it extremely difficult with these sql queries. I sat with stirngbuild until 5am this morning, no kidding. And I just went to bed cus I was so dead tired about it. it's driving me insane Commented Jan 19, 2016 at 12:23
  • 1
    @willie - No, do not run "lots of inserts in a single string". On the other hand, you can have lots of rows in one INSERT; that does run faster (like 10x). Commented Jan 20, 2016 at 1:06
  • 1
    INSERT ...; INSERT ...; is essentially no faster than INSERT ...;, then INSERT ...;. This is faster: INSERT INTO t (a,b) VALUES (1,2), (5,6), ...; Commented Jan 20, 2016 at 1:33

1 Answer 1

1

You are not just doing an insert, but also using on duplicate key update.

I would suggest that you take a bulk-load approach:

  • Put the 18-25,000 things into a file, appropriately delimited.
  • Load the data from the file using load data infile into a staging table.
  • Use the 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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have no idea how to do that type of stuff. how do i output it to a file? is it some special format? like xml? or what? i am a newbie at this, so bare in mind.
still cannot figure this out, @gordon linoff
@TibiaRook - research the term "CSV" or "Comma Separated Values" or "Tab Separated Values". Then look for LOAD DATA INFILE in MySQL. Put those two together to get high speed ingestion. Oh, also, you need to know how to create and write a file from C#.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.