Efficiently Insert Multiple Rows in MySQL – Beginner Guide

目次

1. Introduction

MySQL is one of the database management systems used in many web applications and database management systems. To manage data efficiently, proper INSERT operations are essential. Especially when processing large amounts of data, inserting one row at a time can consume too much time and resources. In this article, we will explain in detail how to insert multiple rows of data at once in MySQL. Using this technique can improve data insertion efficiency and significantly enhance system performance. We will also explain step by step from basics to advanced topics in a way that beginners can easily understand. This content is especially useful for those who face the following challenges:
  • Want to use INSERT statements efficiently
  • Want to reduce data insertion time
  • Want to learn how to handle large datasets
In the upcoming sections, we will comprehensively cover the optimal ways to insert multiple rows in MySQL, including concrete code examples and cautions. In the next section, we’ll start by covering the basics of inserting a single row.

2. Basic INSERT Statement Syntax

When inserting data into MySQL, it’s important to first understand the basic single-row INSERT statement. This syntax is very simple, but it’s the first step to getting comfortable with MySQL operations. Here we’ll explain the basic syntax for inserting a single row of data and provide concrete examples.

Basic INSERT Statement Syntax

When inserting a single row of data into a table, the basic syntax is as follows:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
  • Table name: The name of the table into which data will be inserted.
  • column1, column2, …: Specify the column names in the table that will hold the inserted values.
  • value1, value2, …: Specify the values corresponding to each column.

Example: Inserting Customer Information

For example, suppose there is a table named “customers” as shown below.
idnameemail
1Taro Yamadataro@example.com
The INSERT statement to add a new customer to this table looks like this:
INSERT INTO customers (id, name, email)
VALUES (2, 'Hanako Tanaka', 'hanako@example.com');
After execution, the “customers” table will look like this:
idnameemail
1Taro Yamadataro@example.com
2Hanako Tanakahanako@example.com

How to Omit Column Specification

If you are inserting values for all columns, you can omit the column list. In this case, the order of the values must match the order of the columns defined in the table.
INSERT INTO customers
VALUES (3, 'Ichiro Suzuki', 'ichiro@example.com');

Things to Note

  • Data type matching: The data types of the values being inserted must match the column data types defined in the table.
  • Handling NULL values: If a column allows NULL, you can insert NULL without specifying a value.
  • Default values: For columns with a defined default value, that default is inserted when no value is specified.

Summary

Understanding the basic INSERT statement correctly makes data manipulation in MySQL smoother. Grasping single-row inserts provides the foundation for the next topic, “How to Insert Multiple Rows of Data at Once.”

3. How to Insert Multiple Rows of Data at Once

In MySQL, you can insert multiple rows of data with a single SQL statement. Using this method is more efficient than repeatedly executing INSERT statements and reduces the load on the database. Here we explain the syntax for multi-row inserts and provide concrete examples.

Basic Syntax for Multi-Row Inserts

When inserting multiple rows at once, use the following syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES
(value1_1, value1_2, ...),
(value2_1, value2_2, ...),
(value3_1, value3_2, ...);
  • Enclose each row’s data in parentheses and separate rows with commas.
  • The VALUES clause is written only once.

Basic Example: Inserting Multiple Customer Records

In the following example, multiple rows are inserted into the customers table at once.
INSERT INTO customers (id, name, email)
VALUES
(4, 'Makoto Kato', 'makoto@example.com'),
(5, 'Sakura Mori', 'sakura@example.com'),
(6, 'Kei Tanaka', 'kei@example.com');
After execution, the table is updated as follows:
idnameemail
1Taro Yamadataro@example.com
2Hanako Tanakahanako@example.com
4Makoto Katomakoto@example.com
5Sakura Morisakura@example.com
6Kei Tanakakei@example.com

Efficiency Benefits

  • Reduced Network Load: Inserting multiple rows with a single SQL statement reduces the number of communications with the server.
  • Speed Improvement: Because the insert operation occurs only once, processing becomes more efficient.

Things to Watch Out For

  1. Column count must match the number of values
  • Example: If there are three columns, each row must also have three values, otherwise an error occurs.
  1. Matching Data Types
  • Each value’s data type must match the column’s data type defined in the table.
  1. Avoiding Duplicate Errors
  • If there are primary key or unique key constraints, attempting to insert data with a duplicate key will cause an error.

Error‑Handling Trick: IGNORE Option

Using IGNORE allows the operation to skip rows that cause errors and continue processing.
INSERT IGNORE INTO customers (id, name, email)
VALUES
(7, 'Ryoichi Suzuki', 'ryoichi@example.com'),
(5, 'Duplicate User', 'duplicate@example.com'); -- This row will be ignored

Summary

Inserting multiple rows of data at once allows you to work with the database efficiently. This can lead to reduced processing time and lower server load.

4. Bulk Insertion Method for Large Data Sets

When inserting a large amount of data, regular INSERT statements can become inefficient. In MySQL, you can use the LOAD DATA INFILE command to efficiently load large volumes of data. This method is extremely useful when loading massive data sets into a table in bulk.

LOAD DATA INFILE Basic Syntax

Below is the basic syntax for LOAD DATA INFILE:
LOAD DATA INFILE 'file_path'
INTO TABLE table_name
FIELDS TERMINATED BY ',' -- field delimiter
LINES TERMINATED BY '\n' -- line delimiter
(column1, column2, ...);
  • INFILE: Specifies the path to the file containing the data to be loaded.
  • FIELDS TERMINATED BY: Specifies the delimiter for each field (column) (e.g., comma “,”).
  • LINES TERMINATED BY: Specifies the line delimiter (e.g., newline “\n”).
  • (column1, column2, ...): Specifies the columns into which data will be inserted.

Basic Example: Inserting Data from a CSV File

For example, suppose you have a CSV file like the following (data.csv):
4,Makoto Kato,makoto@example.com
5,Sakura Mori,sakura@example.com
6,Kei Tanaka,kei@example.com
To load this file into the customers table, run the following command:
LOAD DATA INFILE '/path/to/data.csv'
INTO TABLE customers
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(id, name, email);

Using the LOCAL Option

If the CSV file resides on the client side rather than the server, use the LOCAL option:
LOAD DATA LOCAL INFILE '/path/to/data.csv'
INTO TABLE customers
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(id, name, email);

Performance Optimization Tips

  1. Using Transactions
  • Performing the load within a transaction allows you to roll back in case of errors.
   START TRANSACTION;
   LOAD DATA INFILE '/path/to/data.csv' INTO TABLE customers;
   COMMIT;
  1. Temporarily Disabling Indexes
  • Disabling indexes before the load and re-enabling them afterward speeds up the operation.
   ALTER TABLE customers DISABLE KEYS;
   LOAD DATA INFILE '/path/to/data.csv' INTO TABLE customers;
   ALTER TABLE customers ENABLE KEYS;
  1. Processing Data with a SET Clause
  • You can transform data before loading:
   LOAD DATA INFILE '/path/to/data.csv'
   INTO TABLE customers
   FIELDS TERMINATED BY ','
   LINES TERMINATED BY '\n'
   (id, name, @email)
   SET email = LOWER(@email);

Considerations

  • File Permissions: To use LOAD DATA INFILE, the MySQL server must have permission to access the file.
  • Security: When using the LOCAL option, you must take adequate measures against external attacks.

Summary

LOAD DATA INFILE is a very powerful tool for efficiently inserting large amounts of data. Leveraging this method can dramatically improve database operation efficiency.

5. Performance Optimization Tips

When inserting data into MySQL, especially when handling large volumes, optimization to improve efficiency is crucial. This section explains concrete methods to maximize performance.

Using Transactions

By using transactions, you can batch multiple INSERT operations together. This can significantly speed up processing compared to committing each individually. Example: INSERT Using a Transaction
START TRANSACTION;

INSERT INTO customers (id, name, email)
VALUES (7, 'Haruto Sato', 'haruto@example.com'),
       (8, 'Yuki Aoki', 'yuki@example.com');

COMMIT;
Key Points:
  • Execute multiple INSERT statements within a transaction and commit them all at once to reduce disk I/O.
  • If an error occurs, you can roll back the entire operation with ROLLBACK.

Temporarily Disabling Indexes

When indexes are updated during data insertion, processing can slow down. Temporarily disabling indexes and re-enabling them after insertion can improve efficiency. Example: Inserting Data with Indexes Disabled
ALTER TABLE customers DISABLE KEYS;

INSERT INTO customers (id, name, email)
VALUES (9, 'Kaori Tanaka', 'kaori@example.com'),
       (10, 'Shota Yamada', 'shota@example.com');

ALTER TABLE customers ENABLE KEYS;
Things to Note:
  • This technique is effective when bulk inserting large amounts of data.
  • Only secondary indexes can be disabled; primary keys cannot be affected.

Leveraging Batch Processing

Processing data in smaller batches for insertion can improve efficiency. Inserting a massive amount of data at once can increase the risk of memory exhaustion and timeouts. Example: INSERT with Specified Batch Size
-- Insert 100 rows per INSERT
INSERT INTO customers (id, name, email)
VALUES
(11, 'Hiroshi Kato', 'hiroshi@example.com'),
(12, 'Miku Yamamoto', 'miku@example.com'),
... -- Add 98 more rows
(110, 'Rina Suzuki', 'rina@example.com');
Key Points:
  • Adjust the batch size (e.g., 100 rows or 1,000 rows) to keep server load under control.
  • Pay attention to log size and server configuration.

Adjusting Buffer Sizes and Settings

Adjust the MySQL configuration file (my.cnf) to improve insertion performance. Recommended Settings:
  • innodb_buffer_pool_size: Increase the size to manage data more efficiently in memory.
  • bulk_insert_buffer_size: Expand the buffer size used during bulk inserts.
Example: Changing Settings
[mysqld]
innodb_buffer_pool_size=1G
bulk_insert_buffer_size=512M
After changing the settings, restart the MySQL server to apply them.

Summary

To optimize data insertion performance in MySQL, the following methods are effective:
  1. Use transactions for efficiency.
  2. Disable indexes to boost insertion speed.
  3. Employ batch processing to distribute load.
  4. Adjust server settings to extract maximum performance.
By combining these approaches, you can efficiently handle large-scale data insertions.

6. Differences from Other Databases

Data insertion operations in MySQL share similarities with many databases, but also have unique characteristics. This article explains the differences in multi-row data insertion methods between MySQL and other common databases (e.g., PostgreSQL and Oracle).

Comparison of MySQL and PostgreSQL

1. Syntax for Multi-Row Insertion

  • MySQL and PostgreSQL can generally use the same syntax.
MySQL Example:
INSERT INTO customers (id, name, email)
VALUES
(1, 'Taro Yamada', 'taro@example.com'),
(2, 'Hanako Tanaka', 'hanako@example.com');
PostgreSQL Example:
INSERT INTO customers (id, name, email)
VALUES
(1, 'Taro Yamada', 'taro@example.com'),
(2, 'Hanako Tanaka', 'hanako@example.com');
Differences:
  • In PostgreSQL, you can add a RETURNING clause to retrieve the inserted data.
INSERT INTO customers (id, name, email)
VALUES
(3, 'Sakura Mori', 'sakura@example.com')
RETURNING *;

2. Transaction Handling

  • Both support transactions, but PostgreSQL has stricter default settings for transaction isolation levels and data integrity.

Comparison of MySQL and Oracle

1. Method for Multi-Row Insertion

In Oracle, you can use a different INSERT ALL syntax compared to MySQL. MySQL Method:
INSERT INTO customers (id, name, email)
VALUES
(1, 'Taro Yamada', 'taro@example.com'),
(2, 'Hanako Tanaka', 'hanako@example.com');
Oracle Method (INSERT ALL):
INSERT ALL
  INTO customers (id, name, email) VALUES (1, 'Taro Yamada', 'taro@example.com')
  INTO customers (id, name, email) VALUES (2, 'Hanako Tanaka', 'hanako@example.com')
SELECT * FROM dual;
Differences:
  • MySQL inserts multiple rows with a single VALUES clause, whereas Oracle uses the INSERT ALL syntax to insert rows individually.
  • Oracle may require a special virtual table called dual.

Other Differences

1. Data Type Differences

  • MySQL commonly uses TEXT and BLOB types, while Oracle and PostgreSQL use different types such as CLOB and BYTEA.
  • You need to be aware of data type differences when inserting.

2. Error Handling

  • In MySQL, you can use the IGNORE option to ignore errors.
INSERT IGNORE INTO customers (id, name, email)
VALUES (1, 'Duplicate User', 'duplicate@example.com');
  • In PostgreSQL and Oracle, error handling uses dedicated exception mechanisms (e.g., EXCEPTION or SAVEPOINT).

3. Bulk Insertion Methods

  • MySQL offers LOAD DATA INFILE, while PostgreSQL uses the COPY command and Oracle uses a tool called SQL*Loader.

Summary

There are both similarities and differences in multi-row insertion and data manipulation among MySQL, PostgreSQL, and Oracle. Understanding each database’s characteristics helps you choose the most appropriate approach.

7. FAQ

This section explains common questions about inserting data in MySQL and their solutions. It resolves typical doubts readers may have in advance, allowing you to proceed smoothly.

Q1: An error occurred when inserting multiple rows. How should I debug it?

A: If an error occurs when inserting multiple rows, check the following points:
  1. Data type consistency
  • Ensure that the values you insert into each column match the data types defined in the table.
  • Example: make sure you aren’t inserting only numbers into a VARCHAR column.
  1. Number of values matches number of columns
   INSERT INTO customers (id, name, email)
   VALUES
   (1, 'Taro Yamada'), -- Error: missing email value
   (2, 'Hanako Tanaka', 'hanako@example.com');
  1. Constraint violation
  • An error occurs if primary key or unique key constraints are not satisfied.
  • Solution: use INSERT IGNORE or ON DUPLICATE KEY UPDATE to avoid errors.

Q2: What security considerations should be taken when using LOAD DATA INFILE?

A: LOAD DATA INFILE is a powerful feature, but it can pose security risks. Please keep the following in mind:
  1. File access permissions
  • Configure appropriate paths and permissions so that the MySQL server can access the file.
  • Check the SECURE_FILE_PRIV directory setting and use only files within the allowed directory.
  1. Risks of the LOCAL option
  • When using LOAD DATA LOCAL INFILE, use it only between trusted client and server to prevent malicious file reads from a remote server.
  1. Data validation
  • Validate the data in the file beforehand to ensure no invalid data is present.

Q3: What causes performance degradation when inserting large amounts of data?

A: The main causes of performance degradation and their mitigations are as follows:
  1. Index updates
  • Updating indexes during insertion slows down processing.
  • Mitigation: disable indexes before insertion and re-enable them afterward.
  1. Transaction log
  • If each insert operation is committed individually, disk I/O increases and processing slows down.
  • Mitigation: use transactions and commit in bulk.
  1. Insufficient buffer settings
  • If settings like innodb_buffer_pool_size or bulk_insert_buffer_size are too small, data insertion can be slow.
  • Mitigation: adjust the settings to allocate sufficient memory.

Q4: If existing data is present, can multiple-row inserts be performed safely?

A: Yes, you can insert while avoiding conflicts with existing data using the following methods.
  1. Using ON DUPLICATE KEY UPDATE
   INSERT INTO customers (id, name, email)
   VALUES (1, 'Updated Name', 'updated@example.com')
   ON DUPLICATE KEY UPDATE name = VALUES(name), email = VALUES(email);
  1. Using REPLACE INTO
   REPLACE INTO customers (id, name, email)
   VALUES (1, 'Replaced Name', 'replaced@example.com');

Q5: What is the optimal size for batch processing?

A: The optimal batch size depends on the following factors:
  • Server memory and CPU performance.
  • Table structure (presence of indexes, constraints).
  • Data volume and record size.
Generally, aim for about 100 to 1000 rows and adjust as needed. Conduct performance testing to find the optimal size.

Summary

The FAQ provided concrete solutions to common problems and questions encountered when inserting data into MySQL. By using this information, you can perform insert operations more efficiently and safely.

8. Summary

In MySQL, data insertion offers many options, from basic operations to advanced techniques. This article focuses specifically on “multi-row inserts,” explaining efficient and practical methods.

Key Points

  1. Basic INSERT Statement Syntax
  • Inserting a single row is a fundamental MySQL operation, and matching data types and columns is important.
  1. How to Insert Multiple Rows at Once
  • Inserting multiple rows in a single SQL statement reduces network overhead and improves performance.
  1. Bulk Insertion of Large Datasets
  • Using LOAD DATA INFILE allows efficient insertion of massive data sets, though security and configuration considerations are required.
  1. Performance Optimization Tips
  • We covered various methods to improve insert efficiency, such as using transactions, disabling indexes, batch processing, and tuning server settings.
  1. Differences Compared to Other Databases
  • MySQL’s insert methods are simpler compared to PostgreSQL or Oracle, but it’s important to understand techniques that suit each database’s characteristics.
  1. FAQ
  • We provided concrete answers to common questions and error solutions, offering information useful for real-world tasks.

Conclusion

Efficiently inserting data with MySQL is crucial for database operations. Applying the content of this article will not only streamline data insertion but also improve overall system performance. As a next step, we recommend the following practices:
  • Run the SQL statements presented in the article to verify their behavior.
  • Choose an insert method suited to your project and attempt performance optimization.
  • Consult the official MySQL documentation and related books for deeper knowledge.
Streamline data operations with MySQL to drive success in your business and projects.