记录查询语句
在AppServiceProvider boot 中添加
\DB::listen(
            function ($sql) {
                foreach ($sql->bindings as $i => $binding) {
                    if ($binding instanceof \DateTime) {
                        $sql->bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
                    } else {
                        if (is_string($binding)) {
                            $sql->bindings[$i] = "'$binding'";
                        }
                    }
                }
                // Insert bindings into query
                $query = str_replace(array('%', '?'), array('%%', '%s'), $sql->sql);
                $query = vsprintf($query, $sql->bindings);
                // Save the query to file
                $logFile = fopen(
                    storage_path('logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . '_query.log'),
                    'a+'
                );
                fwrite($logFile, date('Y-m-d H:i:s') . ': ' . $query . PHP_EOL);
                fclose($logFile);
            }
        );
再查看 storage 下面的 *_query.log 表
查询指定时间区间
...
use Carbon\Carbon;
/**
 * 月统计
 */
public static function month()
{
    // 前5月的月份
    $first_month = Carbon::parse(Carbon::now()->subMonths(5)->format('Y-m'));
    // 往前面倒6个月
    $first = $first_month->format('Y-m-d');
    $second = $first_month->addMonths(1)->format('Y-m-d');
    $third = $first_month->addMonths(1)->format('Y-m-d');
    $fourth = $first_month->addMonths(1)->format('Y-m-d');
    $fifth = $first_month->addMonths(1)->format('Y-m-d');
    $sexth = $first_month->addMonths(1)->format('Y-m-d');
    $current = $first_month->addMonths(1)->format('Y-m-d');
    $times = compact('first', 'second', 'third', 'fourth', 'fifth', 'sexth', 'current');
    return $times;
}
/**
 * 查询指定时间范围的数据
 * @param $data
 * @return array
 * $type 1 首页访问量 2 app下载量 3 ios
 */
public static function between_total($data, $type =1)
{
    if(!$data) {
        return [];
    }
    $count = [];
    $count[0] = self::query()
        ->where('time', '>=', $data['first'])
        ->where('time', '<', $data['second'])
        ->where('type', $type)
        ->sum('count');
    ......
ab 测试
-n1000            表示总请求数位1000
-c                表示并发用户数为10
Server Software   web 软件名称
			本文由 邓尘锋 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Aug 5, 2019 at 11:17 am