Hi guys I have two queries that would fetch records for me but I am using laravel paginator and I feel its making it slow, I am doing this query for api
/**
* we would search the products 1 table
* with all the request params posted
* we would query only when the request
* params are passed.
* @param object $data This is the request param
* @return eloquent collection
*/
public function searchAdsInProducts1($data)
{
$searchString = $data->get('searchStr');
$categoryId = $data->get('productId');//categories_category_id ie. car, property etc.
$subCategoryIds = $data->get('subCategoryId');//subcat
$subSubCategoryIds = $data->get('subsubCategoryId');//subsubCategoryId
$productsLanguage = $data->get('productsLang');//lang
$page = $data->get('page');
$numberPerPage = $data->get('numberperpage');
$manfactureYearFrom = $data->get('manfactureYearFrom');
$manfactureYearTo = $data->get('manfactureYearTo');
$kmFrom = $data->get('kmFrom');
$kmTo = $data->get('kmTo');
$priceFrom = $data->get('priceFrom');
$priceTo = $data->get('priceTo');
$regionId = $data->get('regionId');
$furnishedTypeId = $data->get('furnishedTypeId');
$numberOfRoomsFrom = $data->get('numberOfRoomsFrom');
$numberOfRoomsTo = $data->get('numberOfRoomsTo');
$cityId = $data->get('cityId');
$categoryAdvertiseTypeId = $data->get('categoryAdvertiseTypeId');
$orderBy = $this->decideOrderBy($data);
$time = $this->decideTime($data);
if(($data->get('page') == 1) AND ($categoryId != 0)){
$sticky = Product::categoriesCategoryId($categoryId)
->where('companies', 1)
->where('sticky', 1)
// ->subSubCategory($subSubCategoryIds)
// ->subCat($subCategoryIds)
->categoryAdvertiseTypeId($categoryAdvertiseTypeId)
->regionId($regionId)
->furnishedTypeId($furnishedTypeId)
->cityId($cityId)
// ->searchString($searchString)
->km($kmFrom, $kmTo)
->price($priceFrom, $priceTo)
->numberOfRooms($numberOfRoomsFrom, $numberOfRoomsTo)
->manufactureYear($manfactureYearFrom, $manfactureYearTo)
// ->language($productsLanguage)
->orderBy('product_id', 'desc')
->take(50)
->get();
}
$ads = Product::categoriesCategoryId($categoryId)
->subSubCategory($subSubCategoryIds)
->subCat($subCategoryIds)
->categoryAdvertiseTypeId($categoryAdvertiseTypeId)
->regionId($regionId)
->furnishedTypeId($furnishedTypeId)
->cityId($cityId)
->searchString($searchString)
->km($kmFrom, $kmTo)
->price($priceFrom, $priceTo)
->numberOfRooms($numberOfRoomsFrom, $numberOfRoomsTo)
->manufactureYear($manfactureYearFrom, $manfactureYearTo)
->lastTwoMonths($categoryId)
->time($time)
->language($productsLanguage)
->orderBy($orderBy, 'desc')
->paginate(50, ['*'], 'page', $page);
if($data->get('isAdsSupported') == 'true'){
$paidAds = $this->getPaidAds($data);
}
if(($data->get('page') == 1) AND ($categoryId != 0)) {
// $data = $ads;
$data = $sticky->merge($ads);
}else{
$data = $ads;
}
return ['ads' => $data, 'paidAds' => $paidAds];
}
When I see db log query it does SELECT count(*) as aggregate
Now I have million records and it would do count *, I feel this would make it slow, is there any other alternative to this for api ? Please suggest thank you.