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 –
- Removing COUNT(*) queries being run during booting
- Added Indexes from PHPMyAdmin on the Database Tables
2
Answers
The query that is taking a lot of time is:
My best guess is that
category
field isn’t indexed. See if yourplaces
table uses an index forcategory
.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.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 :
can you run:
All of those commands, are juste to compare whether there is a significant change.