Skip to content

aglushkov/n1_finder

Repository files navigation

N+1 Finder

Gem Version Build Status Inline docs Code Climate

This gem helps to find N+1 queries.

It works with ActiveRecord and Sequel.

And tested with postgresql, mysql and sqlite.

Installation

gem 'n_1_finder', group: :development

Using

Include middleware to your Rack app:

# Rails
class Application < Rails::Application
  config.middleware.use(N1Finder::Middleware)
  ...
# Grape
class YourAPI < Grape::API
  use N1Finder::Middleware
  ...
# Padrino
class YourAPP < Padrino::Application
  use N1Finder::Middleware

Or use it directly:

  N1Finder.find do
    User.all.map { |user| user.comments.to_a }
  end

Log example:

D, [2016-06-28T11:15:23.019561 #7542] DEBUG -- :
N+1 QUERY DETECTED:
  QUERY: SELECT  "comments".* FROM "comments" WHERE "comments"."user_id" = $1, user_id = [id]
  LINE: /home/andrey/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/pp.rb:187:in `block in pp'
  QUERIES_COUNT: 2
  ORIGINAL_QUERIES:
    SELECT  "comments".* FROM "comments" WHERE "comments"."user_id" = $1, user_id = 618
    SELECT  "comments".* FROM "comments" WHERE "comments"."user_id" = $1, user_id = 947

Configuration

Logger

Default is Logger.new(STDOUT)

Logger can be any instance of Logger class.

  N1Finder.logger = Logger.new('log/n1.log')

ORM

Default is :active_record if you have activerecord gem installed.

Default is :sequel if you have sequel gem installed.

Allowed values are :active_record and :sequel

  N1Finder.orm = :active_record

Running tests

  • Copy spec/.env.example file to spec/.env and set database credentials.
cp spec/.env.example spec/.env
  • Load this variables into your system.
source spec/.env
  • Run tests.
rspec

About

Ruby gem that helps to find N+1 queries.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages