What does your query look like?
Apr 16, 2019
9
Level 3
Laravel relation query taking too much time to load
Hello, I have two models, one for school Member and the other for school Attendance, each one with the corresponding database table.
My goal is to get all school members who have at least one attendance between some particular date, as an example let's take '2019-04-05' and '2019-04-12' (one week)
So in my Members model I have a relation:
public function attendance() {
return $this->hasMany('App\Attendance')
->whereBetween('scan_date', array('2019-04-05', '2019-04-12'));
}
The problem is that the attendance table has like 76000 records now so the query takes about 8 seconds to get the results. Is there a way to make this query faster? maybe limit in some way?
Level 24
Have you analyzed the query with EXPLAIN? Does the scan_date column have an index?
2 likes
Please or to participate in this conversation.