0

The application was working perfectly and all of a sudden I started receiving a timeout error. The original code is not mine and when this problem started occurring I became forced to optimize the application.

Fixes I tried:

  • Caching the "current template", "template options" and "settings" database data in a middleware. This increased the speed drastically because each database row was fetched using its own query (so say we have 18 template options = 18 database queries)
  • Removing any changes I made. Did not work as well, meaning the problem is somewhere else.
  • Upgrading my local MySQL server using an old online installer for Windows.
  • Removing the "company" global scope, which made me 100% sure the scope has nothing to do with it.
  • Running the MySQL queries in the MySQL console, they were fast.

The Company Middleware: Responsible for caching the database data

 public function handle($request, Closure $next) { if (!isset($GLOBALS['CURRENT_TEMPLATE']) || !($GLOBALS['CURRENT_TEMPLATE'] ?? null) instanceof \App\Template) { $GLOBALS['CURRENT_TEMPLATE'] = \App\Template::where('enabled',1)->first();; } if (!isset($GLOBALS['SETTINGS']) || !($GLOBALS['SETTINGS'] ?? null) instanceof Collection) { $GLOBALS['SETTINGS'] = \App\Setting::all(); // Changed this just now in code, it had a copy&paste typo, but no impact at all (used to be \App\Template::all()) } if (!isset($GLOBALS['TEMPLATE_OPTIONS']) || !($GLOBALS['TEMPLATE_OPTIONS'] ?? null) instanceof Collection) { $GLOBALS['TEMPLATE_OPTIONS'] = $GLOBALS['CURRENT_TEMPLATE']->templateOptions()->get(); } if (!isset($GLOBALS['HEADER_MENU']) || !($GLOBALS['HEADER_MENU'] ?? null) instanceof Collection) { $GLOBALS['HEADER_MENU'] = \App\HeaderMenu::all(); } if (!isset($GLOBALS['FOOTER_MENU']) || !($GLOBALS['FOOTER_MENU'] ?? null) instanceof Collection) { $GLOBALS['FOOTER_MENU'] = \App\FooterMenu::all(); } if(class_exists('\App\Helper') && method_exists(new \App\Helper(), 'boot')){ \App\Helper::boot(); } return $next($request); } 

I believe the problem is ongoing somewhere in the queries but I am not sure where yet. I tried debugging with XDebug, reviewing Laravel Debugbar & Clockwork logs and finally Laravel Telescope.


Laravel Telescope Request Log enter image description here

Laravel Telescope Request Queries Log enter image description here

PS: I added the row count per query in this screenshot.

I am currently still working on optimizing the code but as you have already seen, even removing duplicate queries may not be a solution because queries are taking too long anyways.

Please let me know if you have any suggestions for other ways I should debug this problem or even better if you have a solution for it. Please do not hesitate to ask for any more details you may need. Thank you!

UPDATE

After optimizing the queries and removing duplicates, the result is still the same. Queries are taking over 16000ms enter image description here

1 Answer 1

1

Looks like you need some indexes.

INDEX(company_id, enabled) INDEX(template_id) INDEX(slug) 

When LIMIT is used without ORDER BY, the row you get is unpredictable.

(Please use text, not images, to display code.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.