title: How to optimize save to csv file over 700k rows - Laravel
body: <p>I need to write over 700k lines to the csv file. The task should last less than 30 seconds, but it works 70 seconds. How can I optimize this process?</p>
  
  <pre><code>$users = User::all();        
  
      $callback = function () use ($users) {
          $fh = fopen('php://output', 'w');
          fputs($fh, "\xEF\xBB\xBF");
  
          foreach ($users as $user) {
  
              if ($user-&gt;writer) {
                  $books = $user-&gt;books;
                  foreach ($books as $book) {
                      fputcsv($fh, Arr::prepend($data, $book['name']), ';');
                  }
              }
          }
      };
  </code></pre>