美文网首页
写代码要注意代码执行效率php,mysql,laravel

写代码要注意代码执行效率php,mysql,laravel

作者: 高校邦MOOC | 来源:发表于2017-09-08 09:58 被阅读39次

    php查mysql 的时候能不多查就不要多差(当数据量较大时,要做计算的话,还是在mysql里计算,mysql效率肯定比php高)

    下边两端代码实现相同的功能但是在130万数据的时候,相差了3秒。总之自己体会吧
    $this->getQueryBuilder()
                ->selectRaw('COUNT(fan_id) as count,fan_id')
                ->where('tag', 1)
                ->groupBy('fan_id')
                ->having('count', '=', 1) //having
                ->get()->count();
    
    $subQuery = $this->getQueryBuilder()
                ->selectRaw('COUNT(fan_id) as count,fan_id')
                ->where('tag', 1)
                ->groupBy('fan_id')
                ->getQuery();
    //laravel子查询
    \DB::query()->selectSub($subQuery, 'sub')->count();
    

    相关文章

      网友评论

          本文标题:写代码要注意代码执行效率php,mysql,laravel

          本文链接:https://www.haomeiwen.com/subject/zhkgjxtx.html