I'm having an optimization problem, where in need to take n latest
results from database that has a lot of records. I know I can do it by using orderBy
, but I want to avoid it, because orderBy
loops through every record, which increases my load & query times. I found one answer that uses negative number in take()
which basically takes latest records, but it's not working for me, it takes all the records regardless:
Record::all()->take(-5);
This is the output of previous code, which only returns empty Query Builder
instance:
"supplementRecommendationsHistory" => Illuminate\Database\Eloquent\Builder {#2485 ▼
#query: Illuminate\Database\Query\Builder {#2484 ▶}
#model: App\RecommendedSupplement {#2483 ▶}
#eagerLoad: array:2 [▶]
#localMacros: []
#onDelete: null
#passthru: array:17 [▶]
#scopes: []
#removedScopes: []
}
Is there a different aproach to this? Appreciate any help.