How to Check Query Execution Time in Laravel

How to Check Query Execution Time in Laravel

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.

Recommended Laravel Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *