#Laravel APP Hosting
50 messages · Page 1 of 1 (latest)
Using the "Mocha Package" with 2 cores and 4 GB dedicated memory, Just feels like it shouldn't be this slow, page takes 10+ seconds to load sometimes
you should get the laravel debugbar package. it can help break down all the queries/events/requests that occur on a page load and show you which are the slowest
Yeah, sounds like it might be an inefficiency in your code.
If its fast locally with the same size of database, it is a host issue. Otherwise you have probably an unoptimized database/n+1 issues. Try to recreate the issues locally and use debugbar to see what’s up
Locally the same page is about .8-1s load time, (I checked)
with an exported database from the hosting so same data size
Idk how much this is
would It be better to convert the db results into collections and then access those?
It's for a F1 Racing Website,
So I need to proccess their finishing positions per driver per season and then the same for teams
I can't attach the code without it being a file
You should hit the database as little as possible.
Write proper select queries and chunk the results with pagination if it's a paginated UI
Calculate how. Sum?
I can't attach the code without it turning into a .txt file
idk if that's discord or
This is the part that queries the db
once to get all the drivers
and once per driver to get their results
and once per driver to get another result
I just never had bad performance issues on my local machine, Only took ~1sec to load
You're looping through the first result and querying the DB again, once per result
I tried to do the calculating in the query but idk how to do this
Yes for every driver I need their results only
Get it in 1 query not 2000
So would it be better to put all race results in 1 collection and then access that constantly?
Best is to use with in the first query to load the results with the drivers
Yeah but you're gonna have multiple racedrivers rows per driver
it's not 1:1
Oh wait
$drivers = Driver::query()
->select('id', 'name', 'team_id')
->whereExists(function ($query) use ($season) {
$query->from('racedrivers')
->select('driver_id')
->where('driver_id','=',DB::raw('drivers.id'))
->whereIn('race_id',(function ($query) use ($season) {
$query->from('races')
->select('id')
->where('season_id','=', $season->id);
}));
})
->with(['racedrivers' => function($query) use ($driver, $season) {
$query->select('driver_id', 'dnf', 'race_id', 'position')
->whereIn('racedrivers.race_id',(function ($query) use ($season) {
$query->from('races')
->select('races.id')
->whereIn('races.tier_id',(function ($query) {
$query->from('tiers')
->select('tiers.id')
->where('tiernumber', '=', 1);
}))->whereIn('races.season_id',(function ($query) use ($season) {
$query->from('seasons')
->select('seasons.id')
->where('seasonnumber','=', $season->seasonnumber);
}));
}))
->where('driver_id', '=', $driver->id)
->where('dnf', '=', '0')
->where('position', '<', '11');
}])
->get();
Would this put the 'racedrivers' in a seperate variable or the same one?
I think im just gonna try putting the results in collections and using ->where() to filter from collections instead of db
I got it down to this and 600ms load time instead of 4 seconds with debug bar
seems to be working
Slight amount of optimizing
Looks like you still have another N+1
Optimize that one too
I do 0-2 queries per route. There's no reason to be doing 100s
that's cuz I do query per season because of subqueries
SQL can do subqueries natively
Ill try it
Just deployed the optimizations to live and it's infinitely faster, thanks for the help
🤘🏻
I strongly suggest you to add following line to a service provider detect n+1 queries immediately.
AppServiceProvider::boot() method is a good place to add this one.
Model::preventLazyLoading();
Here you can read more about it: https://laravel-news.com/disable-eloquent-lazy-loading-during-development
Holy shit 
2,3k queries 🤯