How to Insert Multiple Rows in MySQL: Efficient Bulk INSERT and Performance Optimization

目次

1. Introduction

MySQL is one of the most widely used database management systems in web applications and database-driven systems. To manage data efficiently, proper insertion (INSERT) operations are essential. Especially when handling large amounts of data, inserting rows one by one can consume excessive time and system resources.

This article explains in detail how to insert multiple rows of data at once in MySQL. By using this method, you can significantly improve insertion efficiency and enhance overall system performance. The explanations progress from basic concepts to advanced techniques, making them easy to understand even for beginners.

This article is particularly useful for those who:

  • “Want to use INSERT statements more efficiently”
  • “Want to reduce data insertion time”
  • “Want to learn how to handle large datasets”

In the following sections, we will comprehensively explain the optimal ways to insert multiple rows in MySQL, including practical code examples and important considerations. In the next section, let’s begin by reviewing the basics of inserting a single row.

2. Basic INSERT Statement Syntax

When inserting data into MySQL, it is important to first understand the basic single-row INSERT statement. Although the syntax is very simple, mastering it is the first step toward becoming comfortable with MySQL operations. Here, we explain the basic syntax and provide concrete examples.

Basic INSERT Syntax

To insert a single row 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 where data will be inserted.
  • column1, column2, …: The column names that will store the inserted values.
  • value1, value2, …: The values corresponding to each column.

Basic Example: Inserting Customer Information

Suppose we have a table named “customers” as shown below.

idnameemail
1Taro Yamadataro@example.com

To insert a new customer record into this table, use the following INSERT statement:

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

Omitting Column Names

If you insert values into all columns, you can omit the column list. In this case, the values must follow the exact order defined in the table schema.

INSERT INTO customers
VALUES (3, 'Ichiro Suzuki', 'ichiro@example.com');

Important Notes

  • Data Type Matching: The data types of inserted values must match the data types defined for each column.
  • Handling NULL Values: If a column allows NULL values, you may insert NULL without specifying a value.
  • Default Values: If a column has a default value defined, it will be inserted automatically when no value is provided.

Summary

Understanding the basic INSERT statement ensures smooth data operations in MySQL. Mastering single-row inserts forms the foundation for the next topic: inserting multiple rows at once.

3. How to Insert Multiple Rows at Once

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

Basic Syntax for Multi-Row Inserts

To insert 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, ...);
  • Wrap each row’s data in parentheses and separate rows with commas.
  • Write the VALUES clause only once.

Basic Example: Inserting Multiple Customer Records

In the following example, multiple rows are inserted into the customers table in a single statement.

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 will be updated as follows:

idnameemail
1Taro Yamadataro@example.com
2Hanako Tanakahanako@example.com
4Makoto Katomakoto@example.com
5Sakura Morisakura@example.com
6Kei Tanakakei@example.com

Why It’s Efficient

  • Reduced network overhead: Because multiple rows are inserted with one SQL statement, the number of round trips between the client and server decreases.
  • Faster execution: Since the insertion is handled in a single operation, processing becomes more efficient.

Important Notes

  1. The number of columns and values must match
  • Example: If there are 3 columns, each row must also contain 3 values, otherwise an error will occur.
  1. Data type consistency
  • Each value must match the data type defined for the corresponding column in the table.
  1. Avoiding duplicate key errors
  • If primary key or unique key constraints exist, attempting to insert the same key values will result in an error.

Tip for Avoiding Errors: The IGNORE Option

By using IGNORE, MySQL will skip rows that cause errors and continue processing the remaining rows.

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

By inserting multiple rows at once, you can operate your database more efficiently. This can help reduce processing time and lower server load.

4. How to Bulk Insert Large Amounts of Data

When inserting a large volume of data, a standard INSERT statement can become inefficient. In MySQL, you can use the LOAD DATA INFILE command to insert large datasets efficiently. This method is especially useful when you need to load big data files into a table in bulk.

Basic Syntax of LOAD DATA INFILE

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 that contains the data to be inserted.
  • FIELDS TERMINATED BY: Specifies the delimiter for each field (column), for example a comma (,).
  • LINES TERMINATED BY: Specifies the delimiter for each line (row), for example a newline (\n).
  • (column1, column2, ...): Specifies the columns into which the data will be inserted.

Basic Example: Inserting Data from a CSV File

For example, suppose you have a CSV file named data.csv like the following:

4,Makoto Kato,makoto@example.com
5,Sakura Mori,sakura@example.com
6,Kei Tanaka,kei@example.com

To insert 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 is located on the client machine 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. Use transactions
  • Running the insertion inside a transaction allows you to roll back if an error occurs.
   START TRANSACTION;
   LOAD DATA INFILE '/path/to/data.csv' INTO TABLE customers;
   COMMIT;
  1. Temporarily disable indexes
  • Disabling indexes before insertion and re-enabling them afterward can speed up the insert process.
   ALTER TABLE customers DISABLE KEYS;
   LOAD DATA INFILE '/path/to/data.csv' INTO TABLE customers;
   ALTER TABLE customers ENABLE KEYS;
  1. Transform data with the SET clause
  • You can transform data before insertion, for example:
   LOAD DATA INFILE '/path/to/data.csv'
   INTO TABLE customers
   FIELDS TERMINATED BY ','
   LINES TERMINATED BY '\n'
   (id, name, @email)
   SET email = LOWER(@email);

Important Notes

  • File permissions: To use LOAD DATA INFILE, the MySQL server must have permission to access the target file.
  • Security: When using the LOCAL option, make sure you have sufficient protection against external attacks.

Summary

LOAD DATA INFILE is an extremely powerful tool for efficiently inserting large amounts of data. By leveraging this method, you can significantly improve the efficiency of database operations.

5. Performance Optimization Tips

When inserting data into MySQL, especially large volumes of data, optimization is essential to improve efficiency. In this section, we explain specific methods to maximize performance.

Using Transactions

By using transactions, you can process multiple INSERT operations together. This approach can significantly improve performance compared to committing each insert 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 inside a transaction and commit them at once to reduce disk I/O.
  • If an error occurs, you can cancel all changes using ROLLBACK.

Temporarily Disabling Indexes

When indexes are updated during data insertion, processing can slow down. Temporarily disabling indexes before inserting data and re-enabling them afterward can improve performance.

Example: Disable Indexes Before Inserting Data

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;

Important Notes:

  • This technique is particularly effective when inserting a large volume of data at once.
  • Only secondary indexes can be disabled; this does not apply to primary keys.

Using Batch Processing

Dividing data into smaller batches for insertion can improve efficiency. Inserting too many rows at once may increase the risk of memory shortages or timeouts.

Example: INSERT with a Defined Batch Size

-- Insert 100 rows per INSERT statement
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 or 1000 rows) to reduce server load.
  • Pay attention to log size and server configuration settings.

Adjusting Buffer Sizes and Configuration

You can improve insert performance by adjusting MySQL configuration settings in the my.cnf file.

Recommended Configuration Parameters:

  • innodb_buffer_pool_size: Increase this value to manage data more efficiently in memory.
  • bulk_insert_buffer_size: Expand this buffer size for large-scale insert operations.

Example: Configuration Changes

[mysqld]
innodb_buffer_pool_size=1G
bulk_insert_buffer_size=512M

After modifying the configuration, restart the MySQL server for the changes to take effect.

Summary

To optimize data insertion performance in MySQL, the following methods are effective:

  1. Use transactions to improve efficiency.
  2. Disable indexes to increase insert speed.
  3. Use batch processing to distribute load.
  4. Adjust server configuration settings to maximize performance.

By combining these techniques, you can efficiently handle large-scale data insertions.

6. Differences from Other Databases

Data insertion operations in MySQL share similarities with other databases, but also have unique characteristics. In this section, we explain the differences in multi-row insertion methods between MySQL and other common databases such as PostgreSQL and Oracle.

Comparison: MySQL vs PostgreSQL

1. Multi-Row Insert Syntax

  • MySQL and PostgreSQL generally use the same syntax for multi-row inserts.

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');

Difference:

  • PostgreSQL allows you to retrieve inserted data using the RETURNING clause.
INSERT INTO customers (id, name, email)
VALUES
(3, 'Sakura Mori', 'sakura@example.com')
RETURNING *;

2. Transaction Handling

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

Comparison: MySQL vs Oracle

1. Multi-Row Insert Method

Oracle provides a different syntax called INSERT ALL for inserting multiple rows.

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 using a single VALUES clause, while 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 data types such as TEXT and BLOB, while Oracle and PostgreSQL use types like CLOB and BYTEA.
  • Be careful about data type differences during insertion.

2. Error Handling

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

3. Bulk Insert Methods

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

Summary

There are both similarities and differences between MySQL, PostgreSQL, and Oracle regarding multi-row insertion and data operations. Understanding the characteristics of each database allows you to choose the most appropriate method.

7. FAQ

In this section, we explain frequently asked questions and their solutions related to data insertion in MySQL. By addressing common concerns in advance, you can proceed with your work more smoothly.

Q1: An error occurred during multi-row insertion. How should I debug it?

A: If an error occurs during multi-row insertion, check the following points:

  1. Data type consistency
  • Ensure that the values inserted into each column match the data types defined in the table.
  • Example: Make sure you are not inserting invalid numeric values into a VARCHAR column.
  1. Matching the number of values and columns
   INSERT INTO customers (id, name, email)
   VALUES
   (1, 'Taro Yamada'), -- Error: missing email value
   (2, 'Hanako Tanaka', 'hanako@example.com');
  1. Constraint violations
  • If primary key or unique key constraints are not satisfied, an error will occur.
  • Solution: Use INSERT IGNORE or ON DUPLICATE KEY UPDATE to avoid errors.

Q2: What security precautions should I take when using LOAD DATA INFILE?

A: While LOAD DATA INFILE is powerful, it may introduce security risks. Pay attention to the following:

  1. File access permissions
  • Ensure the MySQL server has proper access permissions to the file path.
  • Check the SECURE_FILE_PRIV directory setting and use only files located in permitted directories.
  1. Risks of the LOCAL option
  • When using LOAD DATA LOCAL INFILE, use it only between trusted clients and servers to prevent malicious file loading from remote sources.
  1. Data validation
  • Validate the contents of the file in advance to ensure no invalid or malicious data is included.

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

A: The main causes of performance degradation and their solutions are as follows:

  1. Index updates
  • Updating indexes during insertion can slow down processing.
  • Solution: Disable indexes before insertion and re-enable them afterward.
  1. Transaction logs
  • If each insert operation is committed individually, disk I/O increases and performance decreases.
  • Solution: Use transactions and commit in batches.
  1. Insufficient buffer settings
  • If innodb_buffer_pool_size or bulk_insert_buffer_size is too small, insertion performance may suffer.
  • Solution: Adjust configuration settings to allocate sufficient memory.

Q4: Can I safely perform multi-row inserts when existing data already exists?

A: Yes, you can prevent 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 batch size for batch processing?

A: The optimal batch size depends on the following factors:

  • Server memory and CPU performance.
  • Table structure (indexes and constraints).
  • Data volume and record size.

Generally, adjusting between 100 and 1000 rows per batch is a good starting point. Conduct performance testing to determine the optimal size for your environment.

Summary

This FAQ section provided practical solutions to common problems and questions encountered when inserting data in MySQL. By applying this information, you can perform insert operations more efficiently and securely.

8. Conclusion

Data insertion in MySQL offers many options, ranging from basic operations to advanced techniques. This article focused particularly on multi-row insertion and explained efficient and practical methods.

Key Takeaways

  1. Basic INSERT syntax
  • Single-row insertion is fundamental in MySQL, and matching data types and column definitions is essential.
  1. Inserting multiple rows at once
  • Using a single SQL statement to insert multiple rows reduces network overhead and improves performance.
  1. Bulk insertion of large datasets
  • Using LOAD DATA INFILE enables efficient insertion of large volumes of data, though attention to security and configuration is required.
  1. Performance optimization techniques
  • We introduced various ways to improve insertion efficiency, including transactions, disabling indexes, batch processing, and server configuration adjustments.
  1. Differences from other databases
  • While MySQL’s insertion method is relatively simple compared to PostgreSQL and Oracle, understanding the characteristics of each database is important.
  1. FAQ
  • We provided practical solutions to common questions and errors to support real-world use cases.

Final Thoughts

Efficient data insertion in MySQL is crucial for database operations. By applying the techniques covered in this article, you can not only optimize data insertion but also improve overall system performance.

As your next steps, consider the following:

  • Execute the SQL statements introduced in this article and verify their behavior.
  • Select the most suitable insertion method for your project and test performance optimization strategies.
  • Refer to the official MySQL documentation and related technical books for deeper knowledge.

Streamline your data operations with MySQL and contribute to the success of your business and development projects.