#Laravel APP Hosting

50 messages · Page 1 of 1 (latest)

maiden geode
#

I have a Laravel App that I have hosted with "MochaHost" but I feel like it's really slow loading some pages that have to do a lot of calculations, Any recommendations/tips for hosting an app?

#

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

worldly flare
#

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

exotic perch
#

Yeah, sounds like it might be an inefficiency in your code.

soft plinth
#

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

maiden geode
#

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

paper scaffold
#

Bruh

#

You're querying the shit out of your database

maiden geode
#

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

paper scaffold
#

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

maiden geode
#

I have to calculate driver's and team's points

#

It's not just displaying the data

paper scaffold
#

Calculate how. Sum?

maiden geode
#

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

paper scaffold
maiden geode
#

I tried to do the calculating in the query but idk how to do this

maiden geode
paper scaffold
#

Get it in 1 query not 2000

maiden geode
#

So would it be better to put all race results in 1 collection and then access that constantly?

paper scaffold
#

Best is to use with in the first query to load the results with the drivers

maiden geode
#

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

maiden geode
#

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

paper scaffold
#

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

maiden geode
#

that's cuz I do query per season because of subqueries

paper scaffold
#

SQL can do subqueries natively

maiden geode
#

Ill try it

maiden geode
#

Just deployed the optimizations to live and it's infinitely faster, thanks for the help

soft plinth
#

🤘🏻

turbid tree
# maiden geode

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

Laravel News

Learn about the strict lazy loading setting that allows you to disable it entirely for non-production environments.

worldly flare
#

Holy shit OMEGALUL

viscid cliff
#

2,3k queries 🤯