Question

I currently parse a CSV file to insert data into a database, but the problem is that because it's 20 000 rows, it takes very long. Is there a way to insert more lines at once using Laravel migrations?

This is what I am doing at the moment:

foreach ($towns as $town) {
   DB::table('town')->insert(
      array(
         // data goes here
      )
   );
}

I think maybe my question is a bit vague. I want to know what the format is to mass insert multiple items using one query, and if this will actually make a difference in speed?

Was it helpful?

Solution

You can mass insert by filling an array with your data:

foreach ($towns as $town) {
   $array[] = array(... your data goes here...);
}

And then run it just once

DB::table('town')->insert($array);

But I really don't know how much faster it can be. You can also disable query log:

DB::disableQueryLog();

It uses less memory and is usually faster.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top