jeudi 18 février 2021

Optimize Laravel Query for count base on type

enter image description here

In my dashboard, I made these queries on my database

enter image description here

If I just need the counts base on the type, can I just not make a query again?

I'm trying to optimize the page load. Please suggest how would one do this in the cheapest way possible.

public function index(){


    if(Request::get('code') == 'l!ght'){

        if(Request::get('date') == null) {

            $dateTime = new \DateTime();
            $date = $dateTime->format('Y-m-d');

        } else {

            $date = Request::get('date'); 
            
        }

        $q = Norden::whereDate('updated_at', '=', $date);
        $nordens = $q->orderBy('updated_at', 'desc')->get();

        // dd($nordens);
        
        $feeds = Norden::where('type', 'feed')->orderBy('updated_at', 'desc')->get();
        $latestFeed = $feeds->first();

        $countPoop = count(Norden::whereDate('updated_at', '=', $date)->where('type', 'poop')->get());
        $countPee  = count(Norden::whereDate('updated_at', '=', $date)->where('type', 'pee')->get());
        $countFeed = count(Norden::whereDate('updated_at', '=', $date)->where('type', 'feed')->get());


        $t        = strtotime($latestFeed->updated_at);
        $d        = date('Y-m-d H:i:s', $t );
        $t        = $t + 2.5*(3600);
        $nextFeed = date('Y-m-d H:i:s', $t );

        return View::make('layouts.be.norden.index', get_defined_vars());
    
    } else {

        return Redirect::to('/');

    }
}

Should I store the first query result into the array? and parse that array to get the count? Will that be better or worse in term of performance ?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3do53Yj
via IFTTT

Aucun commentaire:

Enregistrer un commentaire