Giter Site home page Giter Site logo

bullet-gem-demo's Introduction

README

This is a bullet gem demo in ruby on rails.

Setup instructions

  1. This project was created with Rails 7.0.8 & ruby 3.1.4
  2. Clone this repository
  3. In main project folder, start the server using ./bin/dev

NOTE: rails s or rails server won't work correctly because I have used TailwindCSS in this and using rails command will build only rails code and not compile css for TailwindCSS.

Understanding N+1 query problem

In Rails, the N+1 query problem refers to a common performance issue that occurs when accessing associations in a loop or batch operation. It arises when code generates one query to retrieve a collection of records and then issues an additional query for each record to fetch associated data. This results in N+1 queries being executed where N represents the number of records retrieved in the initial query.

For example, consider the following scenario where you have a User model that has many posts, and you want to iterate over all users and access their posts:

users = User.all
users.each do |user|
  puts user.hobbies.count
end

If you have 100 users, this code will execute one query to fetch all users and then 100 additional queries to fetch posts for each user individually. This leads to poor performance, especially as the number of users grows.

Solution

To address the N+1 query problem in Rails, you can use eager loading techniques such as includes, preload, or joins to load associated records in advance, reducing the number of queries executed.

users = User.includes(:hobbies)
users.each do |user|
  puts user.hobbies.count
end

With includes, Rails will fetch all users and their associated posts in just two queries (one for users and one for posts), regardless of the number of users. This significantly improves performance compared to the N+1 query approach.

How to idenify if N+1 query issue exists?

For this we can use bullet-gem which specificly alerts the developer when the query is not optimised.

Implementing the code

  1. Run the server
  2. Check that everything works
  3. Follow all TODO's in code step-by-step which should fix all N+1 query problems.
  4. You can find all TODO's by searching TODO string.

Full Video on Youtube

Bullet Gem N+1 Query Problem Ruby on Rails 7

Website

Portfolio Website

bullet-gem-demo's People

Contributors

rt4914 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.