\DB::enableQueryLog();
$s_date = strtotime(date("Y-m-d")."00:00:00");
$e_date = strtotime(date("Y-m-d")."23:59:59");
$SubmitTask = \DB::table('task_submit')->select(['id','stauts'])->where('created_at' , '>=' , $s_date)->where('created_at' , '<=' , $e_date);
// 1.今日已完成领任务
$data['finish'] = $SubmitTask->where('status' , 'Y')->count('id');
// 2.今日已领任务
$data['receive'] = $SubmitTask->where('status' , 'P')->count('id');
// 3.今日待审核任务
$data['waiting'] = $SubmitTask->where('status' , 'W')->count('id');
$info = \DB::getQueryLog();
\Log::info(\DB::getQueryLog());
return $info;
一篇小案例~
很好用的方法~
今天闲来无事,补充下这个地方
查询出你的应用里比较慢的sql语句,只要是页面上有的,都可以查询出来
在\app\Providers\AppServiceProvider.php里面,boot方法里写:
DB::listen (function($query)
{
$sql = $query->sql;
$bingings = $query->bindings;
$time = $query->time;
Log::debug(compact('sql','bingings','time'));
});
2.在log里看就可以了,也可以加个条件甚至超过一些时间的时候才打印这条sql
DB::listen (function($query){
$sql = $query->sql;
$bingings = $query->bindings;
$time = $query->time;
if($time>10){
Log::debug(compact('sql','bingings','time'));
}
});
本文介绍了一种在Laravel框架中监控SQL查询性能的方法,通过启用查询日志和监听慢查询,可以有效定位和优化数据库操作瓶颈。

被折叠的 条评论
为什么被折叠?



