I am trying to insert the data of a query, currently I am doing it with the eloquent ORM of laravel, but it takes a long time since there are approximately 120k of records, so I would like to know if they can help me as it can be done in the same query
This is the query:
$var = DB::select("
select selrs.*, sim.emin, sim.emax
from (select rac.*, sup.zona, sup.sitio, sup.manejo
from datos_rendim_actual rac
left join super sup on (sup.codigo = (rac.fundo::int8 * 1000000 + rac.rodal::int8 ))) selrs
left join sitman sim on (sim.sitio = selrs.sitio and sim.manejo = selrs.manejo)
where selrs.edad >= sim.emin and selrs.edad <= sim.emax
");
This is the dd of $var
array:123921 [▼
0 => {#813 ▼
+"id": 255214
+"fundo": 101
+"rodal": 196826
+"codigo": null
+"edad": 10
+"densidad": "1019"
+"vol_prod1": "0"
+"vol_prod2": "113.95"
+"created_at": null
+"updated_at": null
+"zona": 5
+"sitio": 1
+"manejo": 7
+"emin": 10
+"emax": 20
}
So This is how I insert them:
foreach ($var as $lista) {
$rendimA = new RendimActual;
$rendimA->codigo = $lista->fundo.$lista->rodal;
$rendimA->edad = $lista->edad;
$rendimA->densidad = $lista->densidad;
$rendimA->vol_prod1 = $lista->vol_prod1;
$rendimA->vol_prod2 = $lista->vol_prod2;
$rendimA->vol_prod3 = $lista->vol_prod3;
$rendimA->save();
}
The fields that I have to fill are in the rendim_actual
table and are the following:
codigo
= concat(fundo, rodal) from$var
edad
= from$var
densidad
= from$var
vol_prod1
to n (actually there are 36 but as an example just leave 3) from$var
in terms of time insert by Eloquent takes about 15 minutes, I hope you can help me, ty
I am using laravel-5.8
and postgresql