我准备插入365插入记录,想知道哪个比较好。
1) 逐条插入 1000 个插入查询(对于每条记录)
2) 像这样插入
insert into table name ('field1', 'field2') values (value,value),(value,value),(value,value),(value,value)
我想让第二个执行得更快,它会有用吗。
最佳答案
根据 MySQL INSERT plan我们有以下时间问题:
- Connecting
- Sending query to server
- Parsing query
- Inserting row (1 × size of row)
- Inserting indexes: (1 × number of indexes)
- Closing
因此,多次插入要快得多,因为它会产生Connecting
、Sending query to server
、Parsing query
、Closing
开销只有一次。这也在手册中显示:
- If you are inserting many rows from the same client at the same time, use INSERT statements with multiple VALUES lists to insert several rows at a time. This is considerably faster (many times faster in some cases) than using separate single-row INSERT statements. If you are adding data to a nonempty table, you can tune the bulk_insert_buffer_size variable to make data insertion even faster. See Section 5.1.4, “Server System Variables”.
关于mysql - 执行多个插入查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18606708/