jeudi 26 janvier 2023

How I can pipeline the incomming results in Laravel so I can process them whilst I read them?

I need to run a query in a console command:


use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

use App\Jobs\SendVaccationEmail;

class ProcessDbResult extends Command
{
  protected $signature = "process:entries";
  protected $description = "Bulk Process of results";

   public function handle() 
   {
     $sql = "
        SELECT DISTINCT 
             user_id 
        from 
         (Select user_id from travels where destination = "Bahamas") as bahamas_vac
         LEFT JOIN (Select user_id from travels where destination <> "Bahamas") as non_bahamas_vac ON bahamas_vac.user_id = non_bahamas_vac.user_id
        WHERE
          non_bahamas_vac.user_id = NULL
 ";

     $results = DB:select($sql);
     foreach($results as $result){
       SendVaccationEmail::dispatch($result);
     }
   }
}

But expect the results to be rather large ~ 100.000 records, therefore in order to save memory consumption, I want somehow the database results to be streamed instead being fetched on one go.

What I actually want to do is:

enter image description here

Meaning I do not want to wait for results to be returned but once I have the first result I want to begin to process it.

Is somehow feasible Using laravel? I'm stuck with laravel 5.8.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire