I have a vehicle listing page with filters. I have created a scope for applying the selected filters to the vehicles table. The scope make use of eloquent whereHas method. But it has serious performance issues while executing query because I see each whereHas is converted to subqueries instead of Join. See my scope function below and the query run
public function scopeApplyFilter($query, $conditions, $featured=0)
{
$query->where(function($q) use ($conditions){
if ($conditions->get('content')) {
$q->where('text', 'like', '%'.$conditions->get('content').'%' );
}
if ($conditions->get('condition')) {
$q->where('condition', $conditions->get('condition'));
}
if ($conditions->get('transmission')) {
$q->where('transmission', $conditions->get('transmission'));
}
if ($conditions->get('price')) {
$range = explode('-', $conditions->get('price'));
$q->where('price', '>=',$range[0]);
$q->where('price', '<=',$range[1]);
}
if ($conditions->get('odometer')) {
$range = explode('-', $conditions->get('odometer'));
$q->where('odometer', '>=',$range[0]);
$q->where('odometer', '<=',$range[1]);
}
if ($conditions->get('year')) {
$range = explode('-', $conditions->get('year'));
$q->where('year', '>=',$range[0]);
$q->where('year', '<=',$range[1]);
}
return $q;
})->whereHas('model', function($q) use ($conditions) {
if ($conditions->get('model')) {
$q->where('model_name', $conditions->get('model'));
}
return $q;
})->whereHas('make', function($q) use ($conditions) {
if ($conditions->get('make')) {
$q->where('make_name', $conditions->get('make'));
}
return $q;
})->whereHas('dealer.province', function($q) use ($conditions) {
if ($conditions->get('province')) {
$q->where('province_name', $conditions->get('province'));
}
return $q;
})->whereHas('dealer', function($q) use ($conditions, $featured) {
if($featured){
$q->where('featured', 1);
}
if ($conditions->get('lat')) {
//$conditions->put('distance',5000);
$lat = $conditions->get('lat');
$lon = $conditions->get('lon');
$lat = 53.421879;
$lon = -113.4675614;
$q->select(DB::raw("id, ( 6371 * acos( cos( radians($lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($lon) ) + sin( radians($lat) ) * sin( radians( latitude ) ) ) ) AS distance"))->having('distance','<',$conditions->get('distance')); //3959 for miles
}
return $q;
});
}
The query run is
SELECT *
FROM `vehicles`
WHERE EXISTS (SELECT *
FROM `models`
WHERE `vehicles`.`model_id` = `models`.`id`)
AND EXISTS (SELECT *
FROM `makes`
WHERE `vehicles`.`make_id` = `makes`.`id`)
AND EXISTS (SELECT *
FROM `dealers`
WHERE `vehicles`.`dealer_id` = `dealers`.`id`
AND EXISTS (SELECT *
FROM `provinces`
WHERE `dealers`.`province_id` =
`provinces`.`id`))
AND EXISTS (SELECT id,
( 6371 * Acos(Cos(Radians(53.421879)) *
Cos(Radians(latitude)) *
Cos(
Radians(longitude)
- Radians(
-113.4675614))
+
Sin(Radians(53.421879)) *
Sin(Radians(latitude)))
) AS
distance
FROM `dealers`
WHERE `vehicles`.`dealer_id` = `dealers`.`id`
HAVING `distance` < 200)
AND `status_id` = 1
ORDER BY `created_at` DESC
LIMIT 15 offset 0
501.16 ms
I am using Laravel 5.2. Make, model and dealer are 'belongsTo' relation of vehicle. Everything works fine. But due to performance issues I am forced to rewrite the function using joins. Guys, please help with a best practise to rewrite this function using joins based on conditions like given above. Your inputs are most welcome.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire