bullet
help to kill N+1 queries and unused eager loading
Ruby gem for Rails that detects slow database patterns during development, like N+1 queries, and tells you exactly which line of code to fix and how.
Bullet is a Ruby gem that watches your web application while you work on it and warns you when the database is being used inefficiently. It is designed for use during development, not in production, so the alerts stay internal to your team.
The main problem it targets is called an N+1 query. This happens when your application loads a list of items, then separately queries the database for each item's related data, one by one. For example, loading 100 blog posts and then making 100 separate requests to get each post's comments is an N+1 pattern. Instead of 101 database calls, the application could make just 2. Bullet detects when this is happening and tells you exactly which piece of code is causing it and how to fix it.
Bullet also catches the opposite problem: eager loading that was added to improve performance but is not actually being used. If your code is pre-fetching related data that nothing on the page ever reads, Bullet flags it so you can remove the unnecessary work. It can also suggest when a counter cache, a way to store a running count in the database rather than recounting each time, would speed things up.
When it detects an issue, Bullet can notify you in several ways: a pop-up alert in the browser, a note in the browser's developer console, a log file, a footer added to the bottom of your page, or via third-party services like Slack, Sentry, or Honeybadger. You choose which notification channels to enable in the configuration.
The gem works with ActiveRecord version 4.0 and above, and with Mongoid version 4.0 and above. It can be added to a Rails project with a single generator command, and individual detectors can be turned off if needed. You can also add specific queries to a safe list to suppress warnings for issues you are aware of and have chosen not to fix.
Where it fits
- Catch N+1 database queries in a Rails app during development before they reach production and slow down real users.
- Find and remove unnecessary eager loading that pre-fetches data no template ever reads.
- Get Slack or browser notifications the moment new database inefficiencies appear while you browse the app.
- Identify where adding a counter cache would speed up a page that counts associated records repeatedly.