Inactive Support Notifications (r)

Mar 13, 2024
Logging slow queries with active support notifications

-sidebar-toc>

This article will show the use of Active Support Notifications to record slower queries within the Rails application. In addition, we'll provide guidance on how you can analyze the logs of slow queries in order to identify and fix performance issues.

What's a slow question?

A slow query refers to an application that takes more than a predetermined amount of time run. The definition of slow queries may vary based on the particular application and its performance requirements. The best practice is to judge every query that takes more than 100 milliseconds as slow.

Why is it necessary to record long query?

There are many reasons why it's important, and why it's important to track slow inquiries.

First, slow queries can be a sign of underlying issues with your application's performance. If you observe slow queries then you'll be able spot these issues immediately and act to fix the issue. Prevention is better than treating.

Thirdly, slow queries could aid in improving your application's performance.

Through analyzing the slowness of query logs, it is possible to identify the queries that are creating the greatest performance issues and take steps to improve the performance of these. It is possible to record slow queries in our software by using Active Support Notifications.

What is Active Support Notifications?

Knowing their purpose and utilizing them, you can greatly improve the speed as well as the maintainability and overall quality of your Rails application.

This article outlines the importance on Active Support Notifications and the benefits they bring.

Functionality

  • Instrumentation: You can instrument specific events in your Rails application using the ActiveSupport::Notifications.instrument method. This method requires the name of the user as well as the payload (optional information) along with an element of code which is required to execute.
  • Payload permits users to transmit additional details that are relevant to the particular event that is being tracked. It can be anything like timestamps or user IDs, as well as data query information for databases and any other pertinent information.
  • Subscribers If the block is executed, Active Support sends a message containing the name of the user as well as the payload to every registered subscriber. Subscribers are able to read the message and take specific actions as a result of the event.

Benefits

  • Monitoring performance: By recording critical events, you're capable of monitoring their duration to completion and pinpoint performance problems within your application. This can help you optimize the application's code as as improve the overall efficiency that you can achieve with the Rails application.
  • Troubleshooting: Notifications can give valuable insight into your application's operation and help in identifying problems. Information from the payload may reveal unexpected behavior or problems during runtime.
  • Custom instruments: You are able to extend the functionality that comes from Active Support Notifications by creating custom instruments can be customized and subscriber. It allows you to track occasions that you have tailored to your particular app's requirements.
  • Integration with other libraries: A number of the most well-known Rails libraries, such as Action Cable and ActiveJob, leverage Active Support Notifications to provide the performance information and event-tracking abilities.

How do you track slow queries by using Active Support Notifications

Active Support Notifications is an inbuilt Rails method for recording events that occur within the application. To use Active Support Notifications to log slow queries, the example code below would be placed in an initializer (e.g., config/initializers/error_notifications.rb) to ensure it runs on application startup. You can also add this code to a specific model or controller in order to provide specific logging.

config.active_support.Notifications.subscribe('sql.active_record') do |*args| event = ActiveSupport::Notifications::Event.new(*args) if event.duration > 100 Rails.logger.info "Slow query: #event.payload[:sql]" end end

This program will record every SQL queries that require more than 100 milliseconds.

The information logged will contain information about the SQL statement, as well as the date of the query, and the names of the respective files and the line number on the line where the query was run.

What can you do to analyze the query slow logs

Once you have started logging low-performing queries, you are able to examine the logs to determine which queries are causing the biggest performance issues. There are many methods to analyse the logs of slower queries.

One way is to use instruments similar to Rails Performance Dashboard. Its Rails Performance Dashboard offers a graphic interface to analyze and analyzing the speed of the query logs.

Another way to examine slow query logs is to use an editor for text or a spreadsheet program. It is possible to use an editor for text or a spreadsheet software to sort and filter slow query logs by length, SQL statement, or the file's name.

This will help you pinpoint the issues that create the greatest issues with performance.

What can you do to make your inquiries faster?

After you've identified those queries that are causing most problems for performance, you can take steps to fix the issue. There are numerous ways to improve the performance of your queries.

An alternative is to optimize the performance of your SQL statement. The ability to enhance your SQL statement by using indexes, the addition of constraints, or by using better query techniques.

Utilizing pagination as well as the breaking down of large data sets into smaller pages will enhance the performance of your system as well as to reduce the use of memory.

Consider also using Background Jobs as well as offloading queries that are running for a long time to background jobs (e.g. with Sidekiq, Resque) to prevent web browser request.

Summary

Active Support Notifications are an integrated Rails feature that logs low-performing queries. Recording low-performing queries is an essential component of the performance optimization process that is used by each Rails application. With the help of these logs, it is possible to spot issues with performance early and then take action to correct the issue.

It's possible to utilize Active Support Notifications to log slow queries. You can then look through the logs to identify queries which cause the most problems with performance. When you've found those that are causing the most problems for performance You can then take the necessary steps to fix them.

If you're operating a well-established company and are able to incorporate junior developers into teams, this type of efficiency will help the team to establish better processes through participation with the work. In addition, it will assist the team who supervises senior and junior members, and collaborating to create a stronger and efficient platform. Give your slow query project to a junior developer and they'll be greatly benefited from the project.

Do not allow the problem to exist. Be proactive and prevent slow queries before they can slow your performance. Make sure you are proactive and rather than reactive!

Lee Sheppard

Lee is an Agile Certified Full-Stack Ruby on Rails developer. Over six years in the tech industry He enjoys teaching, coaching Agile and helping other people. Lee is also a speaker at technology-related conferences, as well as has an education in illustration and graphic design.

This post was posted on here