How to use MySQL batch insertion to improve data import speed
Introduction:
When using MySQL for data import, you often encounter a large amount of data, and the traditional one-by-one insertion method is more efficient. Low. This article will introduce how to use the batch insert function of MySQL to improve the speed of data import, and give relevant code examples.
Syntax and examples for using batch inserts
In MySQL, the syntax for batch inserts is as follows:
INSERT INTO 表名 (列1, 列2, 列3, ...) VALUES (值1, 值2, 值3, ...), (值1, 值2, 值3, ...), ...
The sample code is as follows:
import mysql.connector # 连接数据库 conn = mysql.connector.connect(user='root', password='password', database='test') # 创建游标对象 cursor = conn.cursor() # 设置批量插入的数据 data = [ ('Alice', 25, 'female'), ('Bob', 30, 'male'), ('Cathy', 28, 'female') ] # 执行批量插入 insert_sql = "INSERT INTO students (name, age, gender) VALUES (%s, %s, %s)" cursor.executemany(insert_sql, data) # 提交事务 conn.commit() # 关闭游标和连接 cursor.close() conn.close()
The above sample code , we create a list data containing 3 rows of data, and then use the executemany() method to perform batch insert operations. Finally, the transaction is submitted through the commit() method to complete the data insertion. This achieves batch insertion of data.
The above is the detailed content of How to use MySQL’s bulk insert to improve data import speed. For more information, please follow other related articles on the PHP Chinese website!