One of the legacy projects we are dealing with is using old-school mysqli queries all over the place. We have wrapped the application in a Laravel shell, we’ll write about this in another post soon.

One of the major complaints from the customer was the speed of the application. Not surprising if you execute hundreds of queries with complex joins to load one page. To make profiling easier, we added Laravel Telescope to the project. It comes with a nifty feature to analyze queries in Laravel. I thought it would make sense to also include the other queries. Doing so ended up being easier than expected.

Simply add this wherever you want to record a query:

DB::connection()->logQuery($query, [], $this->getElapsedTime($start));

In our project, we created a wrapper around the mysqli class, and are calling this method whenever a query gets executed. Note that the second parameter (bindings) is empty because mysqli does not use prepared statements in this case.