Coder Social home page Coder Social logo

wdi_10_rails_query_optimization's Introduction

ActiveRecord Query Optimization

Shh, don't tell anyone... but all the Rails apps you've been building for the past 9 weeks probably have really bad database performance! Why? Because Rails makes it super easy to write code that causes N+1 queries.

N+1 is a performance issue where, in the process of displaying N records on a page, you end up sending N+1 (or more) SQL queries to the database. This is hugely inefficient, since every round-trip from Ruby to Postgres takes time. It's much better to preload or cache the data you know you'll need, so the number of database round-trips is a small constant number regardless of how many records are being displayed.

A typical example might be displaying a list of articles, which each belong to a user, and wanting to display information about each article's user. We make an initial query for articles in the controller using Article.all, and then every time we call .user on an individual article in the view, another SQL query is sent to retrieve that article's user. If we had 20 articles, we'd be making 21 queries.

Rails has two basic means of solving N+1 issues:

There is also a gem called bullet that can (imperfectly) detect N+1 issues for us, and tell us which of the above techniques we need to use.

Setup

This repo is yet another copy of the example branch from the Week 4 Hacker News project. It now includes seed data that will randomly generate a large-ish number of users, articles, comments, and votes. Run rake db:setup to set up the database, and the seed data will be loaded automatically.

Bullet is set up as per the gem's README โ€“ see config/environments/development.rb.

Important bullet caveat!

Bullet can only detect missing counter caches when we use the .size method to get the number of records in a collection or association โ€“ using .count or .length will not trigger the warning, even if it generates N+1 queries. You should probably be using .size anyway: See the differences here.

wdi_10_rails_query_optimization's People

Contributors

grantovich avatar whtouche avatar

Watchers

James Cloos avatar  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.