Databases 8 min read

Lessons Learned from Using INSERT INTO SELECT for MySQL Data Migration

This article recounts a real-world MySQL data migration incident where using INSERT INTO SELECT caused out‑of‑memory errors and data loss, analyzes why full table scans and transaction locking led to failures, and offers practical recommendations such as indexing and avoiding bulk inserts for large tables.

Top Architect
Top Architect
Top Architect
Lessons Learned from Using INSERT INTO SELECT for MySQL Data Migration

In a high‑traffic MySQL environment, a colleague attempted to migrate millions of rows by either loading data into memory and inserting into a history table or by using INSERT INTO SELECT to let the database handle the operation. The first approach caused an OOM crash, while the second appeared to work in testing but later caused data loss in production.

The root cause was a full table scan triggered by the INSERT INTO SELECT statement, which, under the default transaction isolation level, locked rows one by one. This led to intermittent insert failures and eventually missing payment records.

// 1. Query data to migrate
List<Object> list = selectData();

// 2. Insert data into history table
insertData(list);

// 3. Delete original rows
deleteByIds(ids);

Testing had used realistic data volumes but did not simulate the concurrent load and timing of the nightly migration task, so the issue was missed until it occurred in production.

To prevent similar incidents, the article recommends adding appropriate indexes to the WHERE clause of the SELECT, avoiding full table scans, and being cautious with bulk INSERT INTO SELECT operations on large tables. Proper indexing ensures the query uses an index scan rather than a full table scan, reducing lock contention and execution time.

In summary, while INSERT INTO SELECT can simplify data migration, it must be used with careful consideration of transaction isolation, indexing, and the size of the data set to avoid performance problems and data loss.

data migrationMySQLINSERT INTO SELECTTransaction IsolationDatabase PerformanceFull Table Scan
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.