[英]Laravel Eloquent loads slow
有很多类似的主题,但找不到适合我的解决方案。 该查询加载速度非常慢。
public function getAllBids()
{
return Bid::with('user', 'group')
->join('auct_lots', function ($join) {
$join->on('bids.lot_id', '=', 'auct_lots.lot_id')
->where('auct_lots.company', 'LIKE', '%' . request('brand') . '%')
->where('auct_lots.model_name', 'LIKE', '%' . request('models') . '%')
->where('auct_lots.grade_en', 'LIKE', '%' . request('carGrade') . '%')
->where('auct_lots.auction_name', 'LIKE', '%' . request('auctions') . '%')
->where('auct_lots.model_type_en', 'LIKE', '%' . request('chassis') . '%')
->where('auct_lots.bid', 'LIKE', '%' . request('lots') . '%')
->where('auct_lots.lot_date', 'LIKE', '%' . request('date') . '%');
})
->orderBy('auct_lots.' . request('order_column'), request('order_type'))
->paginate(30);
}
我认为问题在于auct_lots
拥有40,000多个记录...但是我不确定如何重构此代码以更快地工作。
当您有大量数据时,最好使用服务器端缓存。 这将大大提高性能。
步骤1:创建Trait并为缓存中的存储值编写逻辑。 例如:
trait Student{
public function storeStudentDataInCache(){
$students = Cache::rememberForever('studentList', function () {
return Student::
with(['class', 'exams'])
->where('is_published', 1)->orderBy('display_sequence', 'ASC')->get();
});
return $students;
}
}
现在在您的控制器中调用此方法,这将返回数据收集。 因此,您不需要一直运行sql查询来获取数据。 此方法将仅在一次后运行查询。 之后,数据将存储在缓存中,因此它将从缓存文本文件中获取数据的集合。
注意:每当您更新数据时,请不要忘记删除此缓存。
您还可以在需要laravel收集的条件下应用,也可以使用收集的过滤方法来实现更多的过滤逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.