Duplicating a MySQL table, indices, and data
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Copying a MySQL table is easy if you only need rows, and easy to get wrong if you also need indexes, defaults, and production-safe behavior. Different SQL commands duplicate different parts of the table definition. The correct approach depends on whether you want a faithful clone, a reporting snapshot, or a temporary staging table.
Start by Deciding What Must Be Preserved
When people say "duplicate a table," they often mean different things:
- Copy only the data.
- Copy data and column definitions.
- Copy data, indexes, and table options.
- Copy everything plus related objects such as triggers.
MySQL does not have one command that handles every case. The safest general pattern for a near-clone is to create the destination table from the source definition first, then copy rows into it.
CREATE TABLE ... LIKE ... copies the table structure and indexes. The later INSERT ... SELECT ... copies the rows. That split is why the pattern is so useful: you can inspect the new schema before loading any data.
Why CREATE TABLE AS SELECT Is Usually Not Enough
Another common pattern is CREATE TABLE ... AS SELECT ..., often shortened to CTAS. It is attractive because it is concise, but it is not a full clone.
This creates a new table from the query result, but it typically does not preserve the original indexes in the same way as LIKE. It is a good fit for analytical snapshots and temporary reporting tables. It is not the best fit when query performance on the destination table matters immediately after the copy.
If the cloned table is going to serve production reads, missing indexes can turn a "simple copy" into a performance incident.
Verify the Schema Instead of Assuming
Even with the LIKE pattern, verification matters. Table engine, charset, collation, and index names should be checked explicitly when the copy matters operationally.
These statements let you compare source and destination definitions directly. In a migration pipeline, it is worth saving the SHOW CREATE TABLE output into review notes so the clone can be audited later.
You should also remember that some related objects are not necessarily copied the way people expect. Triggers, foreign-key behavior, or environment-specific permissions may still need separate attention depending on the schema and MySQL version.
Copy Large Tables in Chunks When Needed
For a small table, a single INSERT ... SELECT ... is often enough. For a large table, a one-shot copy can create long-running transactions, replication lag, or heavy I/O spikes. In those cases, copy by primary-key range.
Chunking gives you more control over load and progress. It also makes rollback and monitoring easier because you can stop between chunks instead of waiting for one huge statement to finish.
If the source table is still receiving writes while the copy runs, define the consistency model up front. A point-in-time clone is a different job from an eventually consistent copy. That decision affects whether you need transaction isolation, lock planning, or a later reconciliation step.
Validate Data and Query Performance After the Copy
A clone is not done just because the SQL finished successfully. You need to confirm both row completeness and index usefulness.
Row counts are a good first signal, though they are not sufficient for highly critical copies. For important datasets, teams often add checksums, sampled comparisons, or domain-specific validation queries.
The EXPLAIN step matters because it confirms the destination table still behaves like the source under expected queries. A copied table with the wrong schema shape may pass a row-count check and still fail in production.
Use Dump Tools When You Need Repeatability
For controlled migrations, export tools can be a better fit than ad hoc SQL in a terminal. mysqldump lets you separate schema from data so you can review and apply them intentionally.
This is useful when the copy needs to be code-reviewed, archived, or replayed across environments. It also keeps the schema and the data-loading steps explicit instead of burying them inside one live session.
Common Pitfalls
Using CTAS when index fidelity matters is the most common mistake. Another is assuming that because the destination table exists, every piece of metadata copied correctly. Large-table copies also go wrong when people run them at peak load without chunking, replication monitoring, or a clear consistency plan. Finally, many teams verify row counts and forget to verify query plans, which is where missing indexes show up.
Summary
- Use
CREATE TABLE ... LIKE ...plusINSERT ... SELECT ...for a faithful table clone. - Use CTAS only when a lighter snapshot table is acceptable.
- Compare schema definitions explicitly with
SHOW CREATE TABLE. - Chunk large copies so load, lag, and rollback remain manageable.
- Verify both data completeness and post-copy query performance.
Related reading
- Duplicating a MySQL table, indices, and data
- During local development with Kubernetes/minikube, how should I connect to postgres database running on localhost?
- DyanamoDB SCAN with nested attribute
- Dynamically partitioning a table from main to remote mySQL server
- dynamo db local shell doesn't list tables using docker image
- Dynamo db query using contains operator
- DynamoDb - How to do a batch update?
- Dynamodb - Is it bad practice to create lots of partitions with little data?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.