[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 the link_to method
How to use the include? method
How to use the form_with method
How to use the wrapper class
[Java] How to use the File class
[Rails] How to use the map method
[Java] How to use the toString () method
[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)
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 with_option
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 TreeSet
[How to use label]
How to use identity
How to use hashes
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
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 java Optional
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
How to use Big Decimal
[Java] How to use removeAll ()
How to use String [] args
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
How to use GC Viewer
How to use Lombok now