MySql批量插入 [英] MySql Bulk insert
问题描述
我想在MySql数据库中插入一些4K行。我不想激发4k'插入'查询。有没有什么方法我可以只触发一个插入查询来存储数据库中的那些4k行。
I want to insert some 4K rows in the MySql db. I don't want to fire 4k 'insert' queries. Is there any way by which I can fire only one insert query to store those 4k rows in the db.
我在互联网和任何地方搜索我发现用户在做什么从文件批量插入数据库。
I searched on internet and everywhere I found that the users are doing bulk insert into the db from a file.
在我的情况下,我有内存中的数据,我不想先将数据写入文件做批量插入。如果我这样做,那么我将在程序中添加延迟。
In my case, I have the data in the memory and I don't want to first write that data to a file to do bulk insert. If I do that then I will add delay in the program.
推荐答案
你可以写一个 insert
在一次调用数据库中执行多次插入的查询:
You could write a single insert
query that would do several inserts in a single call to the database :
insert into your_table (field1, field2, field3)
values
(value1_1, value1_2, value1_3),
(value2_1, value2_2, value2_3),
(value3_1, value3_2, value3_3)
这里,根据我给出的示例,这个单一查询会在表中插入三行。
Here, with the example I've given, this single query would have inserted three rows in the table.
这篇关于MySql批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!