Developing using Ravel.
The belongsTo
method is used to retrieve table data, but it takes about 4 minutes for $datas
to retrieve 10,000 counts of data.
Do you know how to replace the belongsTo
method when accessing the table in order to speed up the processing?
This may be a rudimentary question, but I appreciate your cooperation.
Current state code
public function sample(Request$request)
{
$query=$this->getDataQuery($request);
$datas=$query->get();
return [
'data' = > $data->map(function($data){
return [
'id' = > $data->id,
'sub_id' = > $data->sub_id,
'hoge_id' = > $data->data_sub->hoge_id,
'hoge_name1' = > $data->data_sub->hoge_name1,
'hoge_name2' = > $data->data_sub->hoge_name2,
'hoge_name3' = > $data->data_sub->hoge_name3,
'result' = > $data->data_sub->hoge?$data->data_sub->hoge_result:',
'category' = > $data->data_sub->hoge?$data->data_sub->hoge_category:',
'status' = > $data->data_sub->hoge?$data->data_sub->hoge_status:',
'info' = > $data->data_sub->hoge?$data->data_sub->hoge_info:',
'date' = > $data->date,
];
}),
];
}
public function data_sub()
{
return$this->belongsTo('data_sub');
}
Based on the code provided alone, it appears that there is probably an N+1 problem in retrieving each item.
with
and load
can significantly reduce the number of queries by doing Eager load.
However, when it comes to handling 10,000 records, it may be necessary to split them up, as memory consumption is likely to be moderate.
The assumption that there is a problem with BelongTo() is questionable.First, we get 10,000 items, store them in memory, and then spin them one by one.I have the impression that the performance is getting worse at this point.So let's improve it first.
Specifically, instead of getting it all at once, you can either read it in chunks or read it one by one on the cursor.The latter performance and the former memory should save more.
https://readouble.com/laravel/6.x/ja/queries.html#chunking-results
In addition, even if you use BelongTo, please load data_sub with EagerLoading.
https://readouble.com/laravel/6.x/ja/eloquent-relationships.html#eager-loading
Also, data is plural form of datum, so the word datas doesn't exist.
© 2024 OneMinuteCode. All rights reserved.