Table of contents

Have you heard of N+1 questions? If you don't, don't worry - it's a common problem in web development, but we have a handy tool to help us identify and solve it. Let’s talk about gem bullet https://github.com/flyerhzm/bullet!

Instead of simply monitoring queries, Bullet acts as a vigilant guardian, scrutinizing your code for performance bottlenecks. It identifies areas for improvement by flagging N+1 query issues, unnecessary eager loading, and suggesting the use of counter caches.

What are N+1 queries?

Imagine writing to all your classmates and their favorite books. You can do this:

  • Get a list of all classmates
  • Ask each classmate what their favorite book is

This is like an N+1 question. You make one request for list (1), then one request for each classmate (N). If you have 30 classmates, that’s 31 requests! That’s a lot of work and can slow down your website.

Add gem bullet to your project

A gem bullet is like a friendly robot that looks at your website and tells you when you have made too many requests with N+1. It’s super easy to use:

  • Add it to your project
  • Use your website as usual
  • Bullet will tell you when it sees an N+1 queries

Best practice is to use Bullet in development mode or custom mode (staging, profile, etc.). For more information, visit https://github.com/flyerhzm/bullet.

How to fix N+1 queries

Once bullet points out a problem, you can fix it. Instead of asking each classmate individually, you can ask them all at once, "Hey class, what are your favorite books?" This is much faster!

Let's break down why asking everyone at once is faster:

  • Fewer trips: When you ask each classmate individually, you're making many separate trips (one per student). But when you ask the whole class at once, you're making just one trip.
  • Time savings: Think of it like delivering mail. If you had to visit each house on a street one by one, it would take much longer than if you could just shout your message to the whole street at once.
  • Network efficiency: In computer terms, each "ask" is like sending a message over a network. Sending many small messages (one per student) is less efficient than sending one larger message (to the whole class).
  • Processing time: Your computer (or database) can often handle one bigger request more efficiently than many small ones. It's like solving one big math problem versus many little ones - the big one might look scary, but it's often quicker than doing lots of small problems.
  • Reduced overhead: Each individual query comes with some extra work (called overhead). By doing one bigger query, you only have this extra work once, not for each student.

In database terms, this approach of getting all the information at once is called "eager loading". It's like filling up your whole backpack with books in one go, rather than walking back and forth to your locker for each class.

From a coding perspective, we use "eager loading" or "includes" objects to get all the information we need at once.

Why should you care?

Blocking N+1 queries can significantly speed up your website. Faster websites keep users happy, use less energy, and can even rank higher in search engines.

Remember that writing efficient code is an important skill for any developer. Tools like gem bullet help us learn and improve our code.

Greetings! Thank you for reading an article titled Stop Website Slowdowns: Identify & Fix N+1 Queries with Gem Bullet which has been authored by Le Huan Vu. In case you wish to gather more insights about the author, you may consider visiting .