screwtape_mk
3 months ago

Optimizing Eloquent Query

Posted 3 months ago by screwtape_mk

I have an autocomplete text box that works well. I need to increase speed of the drop down list. Its too long - you can finish typing before it suggest anything. I have indexed the table and no improvements. So right now I just want to break down the problem into smaller pieces. Lets for now look at the eloquent query itself. Is this the most optimal I could have written it? I got this off the Internet so im not exactly sure why it was written this way it just works- I guess this is how I get to understand the query and optimizing it:

        $query1 = $request->get('term','');
        $countries=\DB::table('T_EMPLOYEE_SERVICE_AREAS');
        if($request->type=='EmpService')
        
            {
                $countries->where('EmployeeService','LIKE',$query1.'%')
                            ->groupBy('EmployeeService')
                            ->get();
            }
      
           $countries=$countries->get();        
        $data=array();
        foreach ($countries as $country) {
                $data[]=array('EmployeeService'=>$country->EmployeeService);
        }
        if(count($data))
             return $data;
        else
            return ['EmployeeService'=>''];

Please sign in or create an account to participate in this conversation.