在laravel中获取多对多关系计数的最佳有效方法是什么?
我有多对多关系的在laravel中获取多对多关系计数的最佳有效方法是什么?,laravel,eloquent,Laravel,Eloquent,我有多对多关系的学生和主题表(透视表是学生_主题) 学生模型 public function subjects() { return $this->belongsToMany(Subject::class, 'student_subject'); } 主题模式 public function students() { return $this->belongsToMany(Student::class, 'student_subject'); } 在这里,我要特别
学生
和主题
表(透视表是学生_主题)
学生模型
public function subjects()
{
return $this->belongsToMany(Subject::class, 'student_subject');
}
主题模式
public function students()
{
return $this->belongsToMany(Student::class, 'student_subject');
}
在这里,我要特别的学生科目。我试过下面的方法,效果很好,但我想用最有效的方法
一,
我通过laravel调试程序检查了SQL查询,如下所示
select * from `students` where `students`.`id` = '10' limit 1
select count(*) as aggregate from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `student_subject`.`student_id` = 10 and `subjects`.`deleted_at` is null
select `students`.*, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` = '10' limit 1
select * from `students` where `students`.`id` = '10' limit 1
select `id`, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` in (10)
select * from `students` where `students`.`id` = '10' limit 1
select count(*) as aggregate from `student_subject` where `student_id` = 10
我通过laravel调试程序检查了SQL查询,如下所示
select * from `students` where `students`.`id` = '10' limit 1
select count(*) as aggregate from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `student_subject`.`student_id` = 10 and `subjects`.`deleted_at` is null
select `students`.*, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` = '10' limit 1
select * from `students` where `students`.`id` = '10' limit 1
select `id`, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` in (10)
select * from `students` where `students`.`id` = '10' limit 1
select count(*) as aggregate from `student_subject` where `student_id` = 10
我通过laravel调试程序检查了SQL查询,如下所示
select * from `students` where `students`.`id` = '10' limit 1
select count(*) as aggregate from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `student_subject`.`student_id` = 10 and `subjects`.`deleted_at` is null
select `students`.*, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` = '10' limit 1
select * from `students` where `students`.`id` = '10' limit 1
select `id`, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` in (10)
select * from `students` where `students`.`id` = '10' limit 1
select count(*) as aggregate from `student_subject` where `student_id` = 10
我通过laravel调试程序检查了SQL查询,如下所示
select * from `students` where `students`.`id` = '10' limit 1
select count(*) as aggregate from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `student_subject`.`student_id` = 10 and `subjects`.`deleted_at` is null
select `students`.*, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` = '10' limit 1
select * from `students` where `students`.`id` = '10' limit 1
select `id`, (select count(*) from `subjects` inner join `student_subject` on `subjects`.`id` = `student_subject`.`subject_id` where `students`.`id` = `student_subject`.`student_id` and `subjects`.`deleted_at` is null) as `subjects_count` from `students` where `students`.`id` in (10)
select * from `students` where `students`.`id` = '10' limit 1
select count(*) as aggregate from `student_subject` where `student_id` = 10
根据以上方法,哪一个是最好的,为什么?或者,如果有任何不同的最佳方法也存在?执行关系()->count()
可能更快
但是,如果您只需要计数,
withCount()
在内存消耗方面应该更好。谢谢您的时间,我需要一种有正确理由的最有效的方法。你的回答没有任何完美的理由。没问题!看看这是否有帮助:根据上面的链接,withCount
比with
的内存效率更高。那么loadCount
?那将是急切加载与惰性加载。由于您没有根据某些动态条件加载计数,因此无需使用loadCount()
运行第二个查询。因此,在您的上下文中,withCount()
也是一个更好的选择。我同意您的观点。但请检查我的SQL查询中的每一点。因此,我认为第四个是最好的(花更少的时间,因为它没有检查与受试者的任何关系)