Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monitor and to log slow queries in Symfony + Doctrine

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.

like image 801
Andrei Herford Avatar asked Nov 28 '25 23:11

Andrei Herford


1 Answers

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.

like image 120
Javier Eguiluz Avatar answered Dec 01 '25 08:12

Javier Eguiluz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!