在laravel中搜索具有多个记录的问题

时间:2018-10-19 10:11:12

标签: mysql laravel

我有一个在laravel中开发的项目。我已经实现了事先搜索。我有 500K 位用户。因此,当我搜索所有记录时,会出现错误,例如无法处理该请求。那么我该如何优化查询呢?或其他解决方案?

数据库查询:

$search_data=User::with(['instrumentsPlayed'=>function($query){
                    $query->select('user_id','name');
                }])
                ->with(['user_music'=>function($query){
                    $query->select('user_id','music_file');
                }])
                ->with(['user_video'=>function($q){
                    $q->select('user_id','video_url');
                }])
                // ->with(['bandMemberDetails'=>function($q){
                //  $q->select('user_id','band_name','genres');
                // }])
                ->leftJoin('user_details','users.id','=','user_details.user_id')
                ->leftJoin('categories','users.member_type','=','categories.id')
                ->leftJoin('sub_categories',DB::raw("FIND_IN_SET(sub_categories.id,user_details.sub_category_id)"),">",DB::raw("'0'"))
                ->leftJoin('genre_music_lists',DB::raw("FIND_IN_SET(genre_music_lists.id,user_details.genres)"),">",DB::raw("'0'"))
                ->leftJoin('user_instrument_played','users.id','=','user_instrument_played.user_id')
                ->leftJoin('instruments','user_instrument_played.instrument_id','=','instruments.id')
                ->leftJoin('user_bands as ub','ub.user_id','=','users.id')
                ->where('users.id','!=',$user_id)
                ->where('users.status','=','active')
                ->select(
                    'users.id','users.member_type','categories.name','users.username','users.city','users.state',
                    'users.first_name','users.last_name','users.country',
                    'user_details.band_name','user_details.travel','user_details.how_far',
                    'users.profile_pic','user_details.experience','user_details.description',
                    DB::raw('GROUP_CONCAT(DISTINCT(genre_music_lists.genres_names)) as genres_names'),
                    DB::raw('GROUP_CONCAT(DISTINCT(sub_categories.subcategory_name)) as subcategory_name'),
                    'instruments.name as instrument_name','user_details.genres',
                    'user_details.venue_name','user_details.address')
                ->groupBy('users.id');
                if(!empty($member_type)){
                    $search_data=$search_data->where('users.member_type',$member_type);
                }

                if(!empty($search_music)){
                    $search_data=$search_data->where(function($query) use($search_music){
                        $query->orWhereRaw(
                                'MATCH(users.username,users.first_name,users.last_name) AGAINST(? IN BOOLEAN MODE)',$search_music);
                        $query->orWhere('user_details.venue_name', 'like',$search_music.'%');
                        $query->orWhere('user_details.band_name', 'like',$search_music.'%');
                        $query->orWhere('user_details.description', 'like',$search_music.'%');
                        // $query->orWhere('categories.name','like','%'.$search_music);
                        $query->orWhere('genre_music_lists.genres_names', 'like',$search_music.'%');
                        $query->orWhere('instruments.name', 'like',$search_music .'%');
                        $query->orWhere('sub_categories.subcategory_name', 'like',$search_music.'%');
                        $query->orWhere('ub.band_name', 'like',$search_music.'%');
                    });
                }


                $search_data=$search_data->where(function($query) use($city,$state,$country,$location,$short_state,$short_country){

                    if(empty($city) && empty($state) && empty($country) && empty($short_state) && empty($short_country)){
                        $query->orWhere('users.city', 'LIKE', $location.'%');
                        $query->orWhere('users.state', 'LIKE', $location.'%');
                        $query->orWhere('users.country', 'LIKE', $location.'%');
                    }else{

                        if(!empty($city)){`enter code here`
                            $query->where('users.city', 'LIKE', $city.'%');
                        }
                        $query->where(function($q) use($state,$short_state){
                            if(!empty($state)){
                                $q->where('users.state', 'LIKE', $state.'%');
                            }
                            if(!empty($short_state)){
                                $q->orWhere('users.state', 'LIKE', $short_state.'%');
                            }
                        });
                        $query->where(function($q) use($country,$short_country){
                            if(!empty($country)){
                                $q->where('users.country', 'LIKE', $country.'%');
                            }
                            if(!empty($short_country)){$q->orWhere('users.country', 'LIKE', $short_country.'%');}
                        });
                    }
                });

                // $search_data=$search_data->toSql();
                // $search_data=$search_data->get();
                // dd($search_data->toArr);
                $search_data=$search_data->paginate($per_page);

我只想知道我将如何获得所有结果,并且花费更少的时间来获得结果?

0 个答案:

没有答案