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

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