2016-02-15 111 views
0

我有表更加50000条记录。多对多关系。Laravel 5雄辩块数据

记录模式:

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(); 

查询执行长。如何优化查询?

+0

尝试增加基本指标到'show'领域。 https://laravel.com/docs/5.2/migrations#creating-indexes –

回答

0

可以分页的:

$records = Records::with('busines')->where('show', '=', 1)->paginate(); 

,并显示分页中的观点:

{!! $records->links() !!} 

或者你可以使用块(见query builder docs “分块结果从表”):

Records::with('busines')->chunk(100, function($records) { 
    foreach ($records as $record) { 
     dump($record->id); 
    } 
}); 
+0

我用数据表的jQuery插件,包https://github.com/yajra/laravel-datatables。如何返回结果在Datatables :: of($ data) - > make(true)中使用块? –

+0

该插件具有支持分页,是否不解决问题了吗?您每页显示多少条记录? –

+0

是分页。 50条记录 –