#145 Improving Partial Loading Performance
8-5-2018

Summary

N+1 queries can make an application slow. Having queries called within views can also slow down an app as well as making it less extendable.
12
rails performance database 11:38

Summary

Gemfilegroup :development do
  ...
  gem 'bullet'
end
config/initializers/bullet.rbif defined?(Bullet)
  Bullet.enable = true
  # Bullet.sentry = true
  # Bullet.alert = true
  # Bullet.bullet_logger = true
  # Bullet.console = true
  # Bullet.growl = true
  # Bullet.xmpp = { :account => '[email protected]',
  # :password => 'bullets_password_for_jabber',
  # :receiver => '[email protected]',
  # :show_online_status => true }
  Bullet.rails_logger = true
  # Bullet.honeybadger = true
  # Bullet.bugsnag = true
  # Bullet.airbrake = true
  # Bullet.rollbar = true
  # Bullet.add_footer = true
  # Bullet.stacktrace_includes = [ 'your_gem', 'your_middleware' ]
  # Bullet.stacktrace_excludes = [ 'their_gem', 'their_middleware', ['my_file.rb', 'my_method'], ['my_file.rb', 16..20] ]
  # Bullet.slack = { webhook_url: 'http://some.slack.url', channel: '#default', username: 'notifier' }
end
controllers/concerns/strict_queries.rbmodule StrictQueries
  class SQLWithViewError < StandardError; end
  module Concern
    extend ActiveSupport::Concern

    included do
      def render(*args, &block)
        return super if production?

        callback = lambda do |name, start, finish, id, payload|
          if !should_ignore_sql_statement?(payload[:name])
            raise SQLWithViewError.new(message(payload[:sql]))
          end
        end

        ActiveSupport::Notifications.subscribed(callback, 'sql.active_record') do
          super
        end
      end

      private

      def production?
        Rails.env.production?
      end

      def should_ignore_sql_statement?(name)
        ['SCHEMA', 'ActionRecord::SchemaMigration Load'].include?(name)
      end

      def message(sql)
        "A SQL request was issued within the view:\n#{sql}"
      end
    end
  end
end
application_controller.rbclass ApplicationController < ActionController::Base
  include StrictQueries::Concern
end
movies_controller.rbclass MoviesController < ApplicationController
  before_action :set_movie, only: [:edit, :update, :destroy]

  def index
    @movies = Movie.all.load
  end

  def show
    @movie = Movie.includes(quotes: :user).find(params[:id])
  end

  ...

end