deansatch
1781
11
Eloquent

Very very slow query...how to speed up

Posted 1 year ago by deansatch

I have this:


        $subQuery = function($q)use($start, $end){
                       $q->whereHas('classdates', function($q)use( $start, $end){
                            $q->whereBetween('datetime',[ $start, $end]);
                        );
                    };

        $subQuery2 = function($q)use($start, $end){
                        $q->whereHas('classes.classdates', function($q)use( $start, $end){
                            $q->whereBetween('datetime',[ $start, $end]);
                        });
                    };
        
        $studios = \App\Studio::
        select('name')->
        withCount(['classes' => $subQuery])
        ->withCount(['bookings' => $subQuery2])
        ->get();

It takes about 20 seconds to run just for a one month date range and I need it to run for full years or even more.

Is there a way I could speed this up?

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

Reply to

Use Markdown with GitHub-flavored code blocks.