#Query optimization

2 messages · Page 1 of 1 (latest)

languid flame
#

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

tardy cove
#

What type of DB? Which indices do your tables have? Are there any hints if you run an EXPLAIN on the query? It can be easier to understand with https://mysqlexplain.com and there's a Laravel package that makes it easier to use. https://github.com/tpetry/laravel-mysql-explain

Optimize MySQL queries with easy-to-understand Visual Explains.

GitHub

Get Visual MySQL EXPLAIN plans with Laravel that are understandable for humans - tpetry/laravel-mysql-explain