LOADING

LaravelModel关联模型注意事项

LaravelModel 关联操作 测试
student_homeworks数据条数:96944
student_hear_records数据条数:32574
原生执行时间:4.36

DB::select('select * from `tbl_student_homeworks` as a  left join `tbl_student_hear_records`as b ON
`a`.id = `b`.student_homework_id where a.task_id = 0 ');

Laravel封装的join执行时间:8.11

 $discovery_count = DB::table('student_homeworks')
->leftJoin('student_hear_records','student_homeworks.id','=','student_hear_records.student_homework_id')
                        ->where('student_homeworks.task_id',0)
                        ->get();

Laravel封装的Model with写法执行时间:25.64

$discovery_count = StudentHomework::where('task_id',0)
                   ->with('hasStudentHearRecords')
                   ->get();

model中:

 public function hasStudentHearRecords(){
        return $this->hasMany('App\StudentHearRecords','student_homework_id','id');
    }

事实告诉我们 当用到join时尽量不要用Model的with方法,因为这真是我见过所有框架里做的最差得了

标签: none

添加新评论