3

I am working on a Symfony 3.4 based project using Doctrine 2.6.

Is it somehow possible to automatically monitor all Doctrine queries and log those with an execution time above some threshold?

I have used blackfire.io and other tools on my dev version to find and fix potential performance issues. However even the best testing is always something different than running the same code on real data in production.

Thus I would like to know which queries take the most time to execute when running in production.

Andrei Herford
  • 17,570
  • 19
  • 91
  • 225
  • Use Datadog APM in production – MylesK Dec 06 '19 at 10:22
  • Well, I'm sorry, but what's wrong with blackfire? We managed to improve a legacy PHP 5 app performance for 30% using just blackfire. Should be much easier with Symfony... – Majesty Dec 06 '19 at 13:19
  • @LuninRoman Blackfire.io is great but obviously not made to monitor a production environment... Datadog APM looks interesting way to complex to only monitor SQL/Doctrine queries. To measure the runtime of a query should not be too hard. Isn't there any build in solution or maybe some events that can be used? – Andrei Herford Dec 06 '19 at 14:09

1 Answers1

2

You can log all Doctrine queries with the doctrine.dbal.logging config option (see https://symfony.com/doc/current/reference/configuration/doctrine.html).

But that won't give you the time spent per query. To do that, you have to create your own logger similar to the one used by Symfony for the debug toolbar (see https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php).

Another possible solution is to use the tools given by your database engine to log slow queries automatically in a dedicated file so you can analyze them later.

Javier Eguiluz
  • 3,987
  • 2
  • 23
  • 44