skip to Main Content

I run a laravel app on my website. Since I have expanded my database’s one table ‘places’ to around 12GB in size, the booting time has increased drastically to ~12 seconds for HomePage and ~15 seconds for SearchPage which queries the ‘places’ table.

The 2 Images below are from HomePage’s Debugbar

Image from HomePage DebugBar Timeline

Image from HomePage DebugBar Queries

The 2 Images below are from SearchPage’s Debugbar

Image from SearchPage DebugBar Timeline

Image from SearchPage DebugBar Queries


I can also see that when I run
php artisan
in terminal of root folder, it takes around ~10 seconds before it displays results

For the above images, we can conclude that the database queries don’t take long despite the size. It’s just the booting time which takes the bigger chunk of the page loading time.

However, when I change the database to a low size one, the booting time is back to <1 sec. Wonders me where the problem is.

I have tried the basic approach to optimizations which includes
php artisan optimize
and checking if OPCache is enabled

Here’s configurations of database: https://justpaste.it/csgo2

SELECT COUNT(*) FROM information_schema.tables; SHOW GLOBAL STATUS; SHOW GLOBAL VARIABLES;

Any suggestions?

EDIT

This is fixed by these 2 things –

  1. Removing COUNT(*) queries being run during booting
  2. Added Indexes from PHPMyAdmin on the Database Tables

2

Answers


  1. The query that is taking a lot of time is:

    SELECT * FROM 'places' WHERE category LIKE '%2184%' AND city_id= 1 LIMIT 16 OFFSET 0;
    

    My best guess is that category field isn’t indexed. See if your places table uses an index for category.

    If your category field uses multiple category id’s, see if you can use a many-to-many table instead. That will improve performance.

    If your category field only uses category_id’s, see if you can make it a foreign key, that will automatically index that column and improve performance.

    If that column could refer to multiple tables, but only refers to one id for each record, create an index. It also will improve your performance.

    Since you haven’t described your table definition, these are my best guesses.

    You also mention that your php artisan is slow. Are you sure you are not executing database queries in __constructor‘s (direct or indirect)? Since that would be a mistake. Your constructors are only to build your classes, not to query data nor to run any business logic.

    Login or Signup to reply.
  2. If you are running in development mode, I can understand, but if you are running in production that may be a serious problem with your app.

    in your .env file set :

       APP_DEBUG=true
       APP_ENV=production
    

    can you run:

    composer install --optimize-autoloader --no-dev
    php artisan config:cache
    php artisan view:cache
    php artisan route:cache
    php artisan event:cache
    

    All of those commands, are juste to compare whether there is a significant change.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search