import bulk data into MySQL

倾然丶 夕夏残阳落幕 提交于 2019-12-04 13:08:50
Mike

LOAD DATA INFILE is very fast, and is the right way to import text files into MySQL. It is one of the recommended methods for speeding up the insertion of data -up to 20 times faster, according to this:

https://dev.mysql.com/doc/refman/8.0/en/insert-optimization.html

Assuming that writing the processed data back to a text file is faster than inserting it into the database, then this is a good way to go.

LOAD DATA or multiple inserts are going to be much better than single inserts; LOAD DATA saves you a tiny little bit you probably don't care about that much.

In any case, do quite a lot but not too much in one transaction - 10,000 rows per transaction generally feels about right (NB: this is not relevant to non-transactional engines). If your transactions are too small then it will spend all its time syncing the log to disc.

Most of the time doing a big insert is going to come from building indexes, which is an expensive and memory-intensive operation.

If you need performance,

  • Have as few indexes as possible
  • Make sure the table and all its indexes fit in your innodb buffer pool (Assuming innodb here)
  • Just add more ram until your table fits in memory, unless that becomes prohibitively expensive (64G is not too expensive nowadays)

If you must use MyISAM, there are a few dirty tricks there to make it better which I won't discuss further.

Guys, i had the same question, my needs might have been a little more specific than general, but i have written a post about my findings here.

http://www.mediabandit.co.uk/blog/215_mysql-bulk-insert-vs-load-data

For my needs load data was fast, but the need to save to a flat file on the fly meant the average load times took longer than a bulk insert. Moreover i wasnt required to do more than say 200 queries, where before i was doing this one at a time, i'm now bulking them up, the time savings are in the region of seconds.

Anyway, hopefully this will help you?

You should be fine with your approach. I'm not sure how much faster LOAD DATA INFILE is compared to bulk INSERT, but I've heard the same thing, that it's supposed to be faster.

Of course, you'll want to do some benchmarks to be sure, but I'd say it's worth writing some test code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!