2016-02-15 110 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條記錄 –