简体   繁体   中英

How can I go about migrating data from one database to another with different structure?

I have about 10,000 data in an old MySQL database written in PHP. This old database has no structure and relationships defined. It's completely legacy design. I'm now working to refactor the entire system of which the tables and their relationships have been completely defined now.

The issue now remains how best to move the data from the old database (written with PHP without framework) to the new (written in Laravel).

Will Laravel commands be a good option where I read data from the old specifying what column is needed and then inserting into the new database?

From the top of my head the following comes to mind:

1. Plain raw SQL

You could write a series of raw sql statements which will read the old database and insert records in the new database. This can be done without the help of an ORM like eloquent.

Advantages:

  • Nothing beats raw SQL in performance, so the migration will run fast

Disadvantages:

  • If the database structure is very different it might be hard to write the correct queries
  • It's easier to forget things like adding primary and foreign keys

2. Laravel commands

You could write one (or multiple) artisan commands which perform the data migration (in steps). This way you can use the DB facade in Laravel to read the old database and use Eloquent to write the data to the new database.

Advantages:

  • Easier to write as you can leverage eloquent models
  • Eloquent takes care of things you otherwise might forget like adding primary and foreign keys

Disadvantages:

  • Raw SQL will probably out-preform the usage of Eloquent.
  • If you have large amounts of data you'll have to optimize your scripts for memory usage. Otherwise you might run into memory limit issues.

So Laravel commands could surely be a good solution depending on how different your data structures are, how large your datasets are and how important performance is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM