Laravel 优化多个whereHas查询

Laravel 优化多个whereHas查询,laravel,eloquent,Laravel,Eloquent,我的模型与属性模型有关系。每个属性都有自己的值。 我执行这样的查询,但速度非常慢 foreach($request->input('attributes') as $id => $value) { $products->whereHas('attributes', function ($q) use ($id, $value) { q->where('attributes.id', $id) ->whereRaw('CAS

我的模型与属性模型有关系。每个属性都有自己的值。 我执行这样的查询,但速度非常慢

foreach($request->input('attributes') as $id => $value) {
   $products->whereHas('attributes', function ($q) use ($id, $value) {
         q->where('attributes.id', $id)
          ->whereRaw('CAST(value AS DECIMAL(10,2)) between ? and ?', $value);
       );
    }

有什么方法可以优化吗?

您确定是查询速度慢吗?它可能会因为其他原因而变慢,例如for循环中的模板中有许多延迟加载模型。如果请求中的属性超过20个,则会出现错误“MySQL服务器已消失”。我想我能以某种方式前进到哪里去。