mysql - 执行多个插入查询

标签 mysql sql

我准备插入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

因此,多次插入要快得多,因为它会产生ConnectingSending query to serverParsing queryClosing 开销只有一次。这也在手册中显示:

  • 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/

相关文章:

php - 从 MySQL 列获取唯一值

MySQL仅在给出完整号码时才匹配区号

sql - 在 SQL Server 2008 中用逗号分割字符串

java - 使用 Hibernate Annotations 的 View 对象的 SQL 错误

sql - 如何优化具有多个相似条件的查询

sql - 为什么需要 "references"权限?

php - 如何使用 PHP 编写动态查询

mysql - 根据条件显示下一个日期

mysql - 使用sts、maven、mysql和tomcat创建本地环境

sql - Web App 建议的 MYSQL 汇总表