Laravel Eloquent 加载缓慢
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;
}
}
现在在你的控制器中调用这个方法,这将return收集数据。所以你不需要一直运行sql查询来获取数据。该方法会运行只查询一次。之后数据将存储在缓存中,因此它将从缓存文本文件中获取数据集合。
注意:每当您更新数据时,请不要忘记删除此缓存。
并且你还可以为laravel集合应用where条件,或者你可以使用集合的filter方法来实现更多的过滤逻辑。
有很多类似的主题,但找不到适合我的解决方案。此查询加载速度很慢。
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;
}
}
现在在你的控制器中调用这个方法,这将return收集数据。所以你不需要一直运行sql查询来获取数据。该方法会运行只查询一次。之后数据将存储在缓存中,因此它将从缓存文本文件中获取数据集合。
注意:每当您更新数据时,请不要忘记删除此缓存。
并且你还可以为laravel集合应用where条件,或者你可以使用集合的filter方法来实现更多的过滤逻辑。