문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top