⚡️ Lifetime Ray licenses available for d h m s
Back to overview

Use Ray to find queries that are slowing you down in your Laravel project

11 August 2023

Image of Tim

Tim

How to do this

Of course, the first thing that you need is the Ray app and the laravel-ray package. If you haven’t installed the package yet you can do this by running:

`composer require spatie/laravel-ray`

Next, you need to add the following to your .env file:

`SEND_SLOW_QUERIES_TO_RAY=true`

You can also do this by publishing the config file and overriding the default there.

php artisan ray:publish-config

You can also configure what should be considered a slow query. By default, Ray will show all queries that are slower than 500ms. But depending on your situation you might want to be more or less ambitious.

For example, let’s say you want to report every query that’s slower than 100ms, then you would add this to your .env file:

RAY_SLOW_QUERY_THRESHOLD_IN_MS=100

An example

Imagine we have a users table and a posts table with tons of records, and we want to shop a set of users that forgot to take out ‘lorum ipsum’ in their post.

By nature, this wouldn’t be the fastest query, especially because we forgot to put an index on the user_id column.

Because the execution time went over the threshold of 100ms we set up Ray will make us aware of this slow query:

Showing slow queries in specific places

If you don’t want to log all slow queries in your application you can also opt to pick your own starting point to start logging. This can be done by calling the showSlowQueries() method in your code:

ray()->showSlowQueries(100);

Other methods to improve your queries

Besides logging slow queries Ray has a ton of other useful tools to improve the database interactions within your app like Showing queries and Counting queries.

Stay in the loop with updates & promotions for Ray

We only send a few emails a year.

Debug without breaking your flow

Ray keeps all your debug output neatly organized in a dedicated desktop app.

A Ray license is valid for 1 year and managed through Spatie. VAT will be calculated during checkout.