Home > database >  Optimize laravel eloquent query
Optimize laravel eloquent query

Time:03-16

hello there i am working on a project with 30000 records, i want to print all the records on the paper, but when excute the following command,it keeps on loading the data until it runs out of memory, keep in mind i am fetching data with three different tables using relations with user_id in common in all the tables, i want to optimize the query, so that i can print all the user records seamlessly

$users= \App\User::with(['withdarawals','deposits'])->get()->groupBy('id');
        dd($users);

CodePudding user response:

The question is if the query is the problem or the size of the result. I suggest three things:

  1. Debug what actually takes long and maybe increase php memory
  2. Add table indexes for quicker database results
  3. Chunk the stream and maybe add the data to a text file or something similar and try to print that.

CodePudding user response:

Move the groupBy() call before the get() call or maybe you meant to call keyBy('id'), as groupBy() doesn't make much sense, because it's used to group arrays of multiple entries with the same value you pass to the method.

  • Related