【问题标题】:Optimize Laravel Query for count base on type优化 Laravel 查询以根据类型进行计数
【发布时间】:2021-02-18 21:45:14
【问题描述】:

在我的仪表板中,我对我的数据库进行了这些查询

如果我只需要基于type 的计数,我可以不再进行查询吗?

我正在尝试优化页面加载。请建议如何以最便宜的方式做到这一点。

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('/');

    }
}

我应该将第一个查询结果存储到数组中吗?并解析该数组以获取计数?就性能而言,这会更好还是更差?

【问题讨论】:

    标签: laravel laravel-5 eloquent


    【解决方案1】:

    您可以使用此代码:

    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);
    
            $latestFeed = Norden::where('type', 'feed')->orderBy('updated_at', 'desc')->first();
    
            $nordenCountTypes = Norden::whereDate('updated_at', '=', $date)
                ->whereIn('type', ['feed', 'pee', 'poop'])
                ->selectRaw('type, count(type) as count')
                ->groupBy('type')
                ->get()
                ->keyBy('type');
            $countFeed = $nordenCountTypes['feed']['count'];
            $countPee = $nordenCountTypes['pee']['count'];
            $countPoop = $nordenCountTypes['pee']['poop'];
    
            $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('/');
        }
    }
    

    注意:确保索引 updated_at 并在 DB 中键入列

    count可以使用count()方法,性能好很多

    喜欢这个

            $countPoop = Norden::whereDate('updated_at', '=', $date)->where('type', 'poop')->count();
    

    【讨论】:

    • 为什么要使用 ->groupBy('type')->keyBy('type'); ?你能在你的回答中解释一下吗?
    • 感谢您的回答,但如果->count(); 很好......为什么您没有将它用作index() 中答案的一部分;`
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 2017-12-30
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 2018-06-26
    相关资源
    最近更新 更多