[Beginner] Discover the N + 1 problem! How to use Bullet

What is the N + 1 problem?

The number of SQL issued increases in proportion to the number of data to be handled. This issue affects performance and should be resolved. As an analogy I often hear, the N + 1 problem is that when you buy a lot of items for shopping, you only pay for each item.

What is a bullet

A library that finds "N + 1 problems".

Introduction method

Gemfile


group :development do
  gem 'bullet'
end

bundle install. Set.

config/environments/development.rb



Rails.application.configure do 
 #Omitted on the way

  config.after_initialize do
    Bullet.enable = true #Enable Bullet gem
    Bullet.alert = true #Pop up JavaScript alert in your browser
    Bullet.bullet_logger = true #Bullet log file (Rails).root/log/bullet.log)
    Bullet.console = true #Warning browser console.Record in log
    Bullet.rails_logger = true #Add alerts directly to Rails logs
  end
end

Solution

If you use bullet, a warning screen and log will appear.

For example, make the relationship between users and posts one-to-many.

user.rb


class User < ApplicationRecord
  has_many :posts
end

post.rb


class Post < ApplicationRecord
  belongs_to :user
end

Outputs the user name for all posts.

Post.all.each do |post|
  puts post.user.name
end
USE eager loading detected
Psot => [:user]
Add to your finder::incluedes => [:user]

You will see such logs and warning messages. Therefore,

Post.inculudes(:user).each do |post|
  puts post.user.name
end

If so, it's OK.

Recommended Posts

[Beginner] Discover the N + 1 problem! How to use Bullet
How to use JUnit (beginner)
How to use the form_with method
How to use the wrapper class
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Processing × Java] How to use the class
[Processing × Java] How to use the function
[Java] How to use the Calendar class
How to use the camera module OV7725 (ESP32-WROVER-B)
[Java] How to use Thread.sleep to pause the program
Output of how to use the slice method
How to use the replace () method (Java Silver)
[Ruby basics] How to use the slice method
How to use Map
How to use rbenv
How to use letter_opener_web
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Rails] I don't know how to use the model ...
[Beginner] How to use devise Change settings from introduction
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
Don't underestimate the N + 1 problem!
[Rails] How to use enum
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy