1 year ago

Laravel relation query taking too much time to load

Posted 1 year ago by Charrua

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?

Please sign in or create an account to participate in this conversation.