Laravel 5雄辩的区块数据
我在表中有50000多条记录。多对多的关系 记录模型:Laravel 5雄辩的区块数据,laravel,laravel-5,eloquent,query-optimization,Laravel,Laravel 5,Eloquent,Query Optimization,我在表中有50000多条记录。多对多的关系 记录模型: public function busines(){ return $this->belongsToMany('App\Busines', 'busines_record', 'record_id', 'busines_id'); } 业务模式: public function records() { return $this->belongsToMany('App\Reco
public function busines(){
return $this->belongsToMany('App\Busines', 'busines_record', 'record_id', 'busines_id');
}
业务模式:
public function records()
{
return $this->belongsToMany('App\Record', 'record_id');
}
我的问题是:
$data = Records::with('busines')->where('show', '=', 1)->get();
查询是长时间执行的。如何优化查询?您可以将其分页:
$records = Records::with('busines')->where('show', '=', 1)->paginate();
并在视图中使用以下内容显示分页:
{!! $records->links() !!}
或者您可以使用chunk(请参阅“从表中分块结果”):
尝试将基本索引添加到
show
字段。我使用datatables插件jquery,包。返回结果如何在Datatables::of($data)->make(true)中使用chunk?该插件支持分页,这并不能解决您的问题吗?每页显示多少条记录?
Records::with('busines')->chunk(100, function($records) {
foreach ($records as $record) {
dump($record->id);
}
});