【发布时间】:2020-09-24 20:00:46
【问题描述】:
我在 laravel-5.7 中有一个作业表,其中表中的 5000 条记录也有一些通过 HasMany() 或 HasOne() Relation 来的关系记录。我尝试了多种类型的 Eloquent Queries 以获得快速的结果。但是 Postman 结果时间变为 10200 毫秒到 10700 毫秒,但是当我将其直接显示给邮递员时,我将其变为 500 毫秒到 1100 毫秒。我想在绑定 Laravel 资源或普通数组后大约 800 毫秒内得到它。
问题是,当我尝试直接显示 Eloquent 结果时,它大约需要 600 毫秒到 1000 毫秒。但是当我绑定到一个数组并在邮递员中显示时,它需要 6200 毫秒,为什么?不知道?
$page = $req->page ?$req->page:1; // set starting value for look query limit.
$user = Auth::user()->student()->first();
$studentProfile = Auth::user()->student()->first();
// collecting all homework id that have assigned to student.
$studentHWList = StudentHomeWork::where("student_id",$studentProfile->id)
->select('home_work_id')
->get()->pluck('home_work_id');
// collecting page by page of homework id.
$hwLimitList = Homework::where('session_code', dnc($req->require('sid')))
->whereIn('id',$studentHWList )
->where('approved', '1')
->select('id')
->orderBy('updated_at','desc')
->get();
$hwIndexes = $hwLimitList->pluck('id')->forPage($page,$this->recordLimit);
$paginated = Homework::whereIn('id', $hwIndexes)
->with( "user:id,username,name",
'subject:id,subject_name,subject_code',
'approveByUser','publishBy')
->with(["likes"=>function($erw){
$erw->select('id','home_work_id','complete_status','likes')
->where("student_id", $studentProfile->id);
}])
->with(['comment'=>function($qur){
$qur->where('parent_id',0)
->where('user_id',$user->id);
}])
->orderBy('id','desc')
->get( );
if( count($paginated))
{
$paginationData = customPagination('getAllHW',$hwLimitList , $page , $this->recordLimit , $user, $studentProfile );
return response()->json(["error"=>0,"errmsg"=>"","paginationData"=>$paginationData ,
"response"=>['homework_list'=>$this->customResourceHWBinding($paginated , $req )],'auth'=>userType()]);
private function customResourceHWBinding($queryData , $request, $user, $studentProfile )
{
$document_list =[]; $is_seen=0; $resultData =[];
foreach ( $queryData as $query )
{
if( count($query->document) )
{
foreach($query->document as $document){
if( $document->changed_filename )
{
$file=""; $fileName ="";
$path =env('AWS_URL')."/uploads/".dnc($request->header('dbauth'))."/".$query->session_code."/homeWorks/";
if(is_s3FileExist( $path.$document->changed_filename ) )
{
$fileName =$document->changed_filename;
}
$document_list[] = [
'oname'=> $document->changed_filename,
'ext'=>$fileName?explode('.', $document->changed_filename):"",
'url'=>$file,
'file_url'=>$document->changed_filename?$path.$document->changed_filename:""
];
}
}
}
$resultData[] = [
'id'=>enc($query->id),
'ids'=>$query->id,
'pin_user_id'=>"",
'pin_enabled'=>0,
'created_by'=>$query->user->name,
'created_by_image'=>getUserImage($query->user,$query->user->privilege,$request),
'assignment_date'=>getDateFormat($query->assignment_date,0),
'assigment_date_edit'=>"",
'submission_date'=>getDateFormat($query->submission_date,1),
'submission_date_edit'=>"",
'class_code'=>$query->class_code,
'subject'=>$query->subject?$query->subject->subject_name:"",
'topic'=>$query->topic,
'is_student_seen'=> $this->studentHWSeen($query, $user, $studentProfile),
'updated_at'=> date('d-m-Y H:i:s' , strtotime($query->updated_at)),
'approved'=>$query->approved,
'approve_by'=> '',
'can_approve'=>0,
'comment_count'=>0,
'total_like'=>0,
'documents_count'=>count($document_list)?count($document_list):0,
'is_draft'=> $query->draft?$query->draft:0,
];
}
return $resultData;
}
private function studentHWSeen( $query , $user, $studentProfile)
{
if(count($query->studentSeen))
{
foreach($query->studentSeen as $seen){
if( user->privilege == 1 )
{
if($seen->student_id == $studentProfile->id )
return 1;
}
}
}
return 0;
}
我尝试使用 Resource,但也需要 3 秒以上。我尝试了太多其他优化解决方案但在我的情况下不起作用。有人告诉使用查询生成器而不是 Eloquent 来优化查询。在这里找到Optimising Laravel query。这对我来说是个好答案吗?我不知道。请帮帮我。
【问题讨论】:
-
好吧,如果 eloquent 很快,但是 postman 显示结果延迟,那么问题就出在
customResourceHWBinding()。尝试找出代码的哪一部分引起了问题。也许函数is_s3FileExist,它必须连接到S3并检查文件是否存在。 -
向我们展示生成的 SQL。
-
我在这里查看。问题在这里 customResourceHWBinding 我知道。 is_s3FileExist 已删除,但没有得到任何改进。还有其他方法可以将所有数据绑定到 Array 或 Laravel Resource 以表示 API。
-
我正在寻找能够快速显示我的结果的任何技术。因为它在 500 毫秒内来自数据库服务器,但在 5 到 10 秒内生成 API....
标签: php laravel-5 eloquent query-optimization laravel-query-builder