I am writing this query and this query is taking 20 second to complete this ajax request
public function getdidno($arr = null)
{
$query = DB::table('did_numbers as d')
->leftJoin('distribute_config as dc', 'dc.did', '=', 'd.did_id')
->leftJoin('landing_number as l1', 'l1.did_1', '=', 'd.did_id')
->leftJoin('landing_number as l2', 'l2.did_2', '=', 'd.did_id')
->select('d.*')->distinct()
->whereNull('dc.did')
->whereNull('l1.did_1')
->where('d.trunk_name', '!=', '')
->whereNull('l2.did_2');
if (!is_null($arr)) {
$query->whereNotIn('d.did_id', explode(',', $arr));
}
$result = $query->get();
return response()->json($result);
}
please suggest me how to optimize this query