Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

taishi's avatar

Get Accurate Query Execution Time

Hi,

Good day, I am trying to fetch all of the queries being ran on every request with they're execution time.

It works on the AppServiceProvider.php using DB::listen. Apparently, the variable $time doesn't seem to match the execution time it provides when I run the query on my database, the one being provided by the DB::listen seems to be longer than what I have on my database execution.

Not really sure if I'm doing this the wrong way. I am using Laravel 5.4 btw.

TIA!

0 likes
5 replies
36864's avatar

I'm fairly sure the time you're measuring includes data transmission time, so it would always be higher than execution time.

florianhusquinet's avatar

You can use the barryvdh/laravel-debugbar package to see every query made on every page more easily. Making a query directly into your database is often quite a bit faster than doing it through an ORM as @36864 said.

1 like
taishi's avatar

Yeah, that's true, but some of the result time are not realistic, like I have a log that indicated a 9.xs query which my application just executed like 1 or 2 sec.

taishi's avatar

@florianhusquinet , that's good if you'd like to see it thru browser. What I'm trying to achieve have it on a log and monitor queries that has long exec time. This is a way for me to see which query needs to be optimized.

36864's avatar

Have a look at mnabialek/laravel-sql-logger

It even lets you log slow queries separately. If nothing else it might give you a hint as to how to make your own logger.

Please or to participate in this conversation.