【问题标题】:Retrieve collection with multiple withCount() takes too long to load (Laravel)使用多个 withCount() 检索集合加载时间太长 (Laravel)
【发布时间】:2022-12-04 14:38:35
【问题描述】:

I\'m working on a query that the model needs to count some data with different conditions from its related tables. Currently I\'m using multiple withCount() with different condition to get the desire outcome.

When I paginate the query is fine, but one of my requirement is to retrieve all rows and process them in one go. When I retrieve all, the performance is very slow. The total record is only 2000 rows.

$products = Product::withCount([\'tableA AS tableA_condition_A\' => function($query){
   $query->whereIn(\'status\', [1,2,3])->whereNotIn(\'type\', [1,2,3]);
}])
->withCount([\'tableA AS tableA_condition_B\' => function($query){
   $query->whereIn(\'status\', [1,2,3])->whereIn(\'type\', [1,2,3]);
}])
->withCount([\'tableB AS tableA_condition_A\' => function($query){
   $query->whereNotIn(\'type\', [1,2,3])->whereHas(\'tableC.tableD\', function($subquery){
        $subquery->whereIn(\'group\', [1,2,3])->whereIn(\'state\', [1,2,3]);
   });
}])
->withCount([\'tableB AS tableA_condition_A\' => function($query){
   $query->whereIn(\'type\', [1,2,3])->whereHas(\'tableC.tableD\', function($subquery){
        $subquery->whereIn(\'group\', [1,2,3])->whereIn(\'state\', [1,2,3]);
   });
}])
// has a few more similar withCount() with condition
->get();

// before go into foreach, the get will take a long time

// after retrieve products withCounts data, process
foreach($models as $model){
   // process...
}

I\'ve tried several ways to optimize my query but this gives the best performance of all.

I know each row are executing queries that\'s why its performance is slow. Is there any way or good practice on improve the performance on this? The code above is just an example. Thank you in advance!

My first way is I tried to use foreach() to calculate each row\'s related tables, but this has the worst performance of all.

Second way is to use with() eager loading from its related tables and do collection filtering, but is performance doesn\'t improve.

The third way is the above code, that has the best performance of all.

    标签: php mysql laravel performance query-optimization


    【解决方案1】:
    猜你喜欢
    • 2023-04-06
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    相关资源
    最近更新 更多