To check query execution time in Laravel; For this, you can use getQueryLog()
, when you use this function you will get all the information related to the query execution.
If you want to get or check any information related such as execution time, query log, view sql core query, etc, to query execution in Laravel application, then you can use Laravel Eloquent’s build method getQueryLog() for this.
How to Check Query Execution Time in Laravel
Steps to check and get query execution time, log, error, etc in laravel 11, 10, 9:
1. Enable Query Logging: It is disabled by default in laravel applications, To enable query log for print last executed query in laravel, you can do it by using \DB::enableQueryLog()
method on laravel, for example:
\DB::enableQueryLog()
2. Get Query Log: After executing the desired query, you can get or retrieve the executed queries and their information by using getQueryLog()
method. For example:
$queryLog = \DB::getQueryLog()
3. Check Query Execution Time: The $queryLog
variable now contains an array of executed queries with information such as query, bindings and time; You can print it and display query execution time; for example:
$queryLog = \DB::getQueryLog() $executedQuery = end($queryLog); $executionTime = $executedQuery['time']; dd($executionTime);
Each query in the log is represented as an associative array with the following information:
query
: The actual SQL query executed.bindings
: An array of parameter bindings used in the query.time
: The execution time of the query.
Here is the all code to check and get query execution time in laravel:
\DB::enableQueryLog(); $queryLog = DB::getQueryLog(); $executedQuery = end($queryLog); $executionTime = $executedQuery['time']; dd($executionTime);
Conclusion
That’s all; You learned how to quickly get query execution time with the help of the Laravel getQuerylog() eloquent method.