我需要你的帮助,因为我希望尽可能少地优化这些查询...
我想获得以下警报的计数:
companies -> sites -> interventions -> purchases -> alerts
我想得到以下行动的计数:
contractors -> actions
companies -> actions
我执行以下操作,但每页上的7个请求并不是很好:/
$companies = Company::where('id', '=', Auth::user()->userable_id)->orWhere('company_id', '=', Auth::user()->userable_id)->pluck('id')->toArray();
$contractors = Contractor::where('id', '=', Auth::user()->userable_id)->orWhere('company_id', '=', Auth::user()->userable_id)->pluck('id')->toArray();
$cp = array_merge($companies, $contractors);
$sites = Site::whereIn('company_id', $companies)->pluck('id')->toArray();
$interventions = Intervention::whereIn('site_id', $sites)->pluck('id')->toArray();
$purchases = Purchase::whereIn('intervention_id', $interventions)->pluck('id')->toArray();
$bubblecounts['alerts'] = Alerts::whereIn('purchase_id', $purchases)->where('status', 0)->whereDate('alert_date', '!=', '0000-00-00 00:00:00')->count();
$bubblecounts['actions'] = Action::whereIn('actionable_id', $cp)->where([['status', 0], ['alert_date', '!=', null]])->count();
你能帮帮我吗?谢谢!
答案 0 :(得分:0)
我能想到的一个优化是:
$alerts = Alerts::whereIn('purchase_id', $purchases)->where('status', 0)->whereDate('alert_date', '!=', '0000-00-00 00:00:00')->count();
$actions = Action::whereIn('actionable_id', $cp)->where([['status', 0], ['alert_date', '!=', null]])->count();
$bubblecounts['alerts'] = $alerts;
$bubblecounts['actions'] = $actions;
而不是获取整行,它只获得计数。如果没有大量的警报和操作,您将获得良好的性能提升。