Mick79's avatar

Advice on making my code more efficient

My app is... let's call it a music repository. I am at the stage now where some of my clients are storing hundreds of tracks. I never expected this when I started this thing as a hobby / weekend fuckabout project - however here we are.

Some of my power users are getting timeout errors now when they view their tracks page as the system is away getting hundreds of tracks plus some data on them like play counts etc.

For the sake of argument let's say that pagination is not a viable option. Is there a more efficient way for me get and display these tracks?

My current code is below:

 public function index()

    {

        $user = Auth::user();
        $userid = $user->id;
        
        $trackcount = Track::countTracks($userid);
        
        $tracks = Track::where([
            ['user_id', $userid],
            ['group_token', null],
            ['status', 1]
        ])->orderBy('created_at', 'DESC')->get();
       
        return view('tracks',  compact('tracks', 'trackcount'));

    }
0 likes
2 replies
Mick79's avatar

@tykus This solved it. I thought I had my indexing covered but on that "where group_token = null", there was no index on group_token column.

I added that and now it's zippy!

Thanks for the nudge!

Please or to participate in this conversation.