jericopulvera's avatar

How do I transfer large table data to another table with the same column quickly?

I'm creating a Product Excel Import which process thousands of products.

My plan is to save it to product_staging table first then when the product_staging table is

completed I will then swap it to my products table. so I it would feel like it has no downtime.

how do I achieve this?

0 likes
6 replies
phpMick's avatar

Not sure of the best way but these would work:

Maybe just with SQL? INSERT using a SELECT. Wrap it in a transaction, so you can roll back.

But if that is a bit scary (or on a production box), just knock up some code in a controller to iterate through the temp table, then create them in the products table.

NickeyGod's avatar
CREATE TABLE x LIKE y;
INSERT INTO x SELECT * FROM other_db.y;
1 like
mcarazzo's avatar

The best approach is to manage it using SQL. If you are using a MySql DB, you can store information into temporary staging table, validate or clean what you need and then move all the information into the definitly table.

Code may change depending if you have one table per upload or you will manage an ID in 1 table for all the stagin uploads

DB::statement("Insert into table Select * from table_staging"); DB::commit();

jericopulvera's avatar

Is this approach possible and good?

Product Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = env('TABLE_PRODUCTS');
}

.env

TABLE_PRODUCTS=products

Then I would try to change the .env to TABLE_PRODUCTS=product_staging when the excel has been saved successfully in the product_staging table Is this approach possible?

mcarazzo's avatar

Why do you want to change your table name from .env file?

Can you use 2 different tables just using a prefix in the table name?

Cronix's avatar
Cronix
Best Answer
Level 67

another option is to use db triggers on the table and not use php at all. Just have the database do it automatically and copy from your staging table to the products table when it meets your criteria as for when it's 'completed'. It will just do it behind the scenes automatically and save system resources and less code.

Please or to participate in this conversation.