Skip to content

Instantly share code, notes, and snippets.

@naoty
Created December 16, 2020 09:35
Show Gist options
  • Save naoty/3c2e02d90743d6805da41e70dd515100 to your computer and use it in GitHub Desktop.
Save naoty/3c2e02d90743d6805da41e70dd515100 to your computer and use it in GitHub Desktop.
eager_loadでSELECTするカラムを絞れるか検証する
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'activerecord'
gem 'sqlite3'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :authors do |t|
t.string :name
end
create_table :books do |t|
t.string :title
t.integer :total_page_count
t.text :body
t.references :author
end
end
class Author < ActiveRecord::Base
has_many :books
has_many :books_metadata, -> { select(:id, :author_id, :title, :total_page_count) }, class_name: 'Book'
end
class Book < ActiveRecord::Base
belongs_to :author
end
class EagerLoadTest < Minitest::Test
def test_assert
author = Author.create(name: 'naoty')
author.books.create(title: 'dummy', total_page_count: 100, body: 'dummy')
sql1 = Author.eager_load(:books).to_sql
sql2 = Author.eager_load(:books_metadata).to_sql
assert sql1 != sql2
end
end
@naoty
Copy link
Author

naoty commented Dec 16, 2020

selectでカラムを制限した関連を定義してeager_loadに指定してみたけど効果なさそうだった。

-- create_table(:authors)
D, [2020-12-16T18:32:13.540410 #13646] DEBUG -- :    (0.8ms)  SELECT sqlite_version(*)
D, [2020-12-16T18:32:13.540931 #13646] DEBUG -- :    (0.2ms)  CREATE TABLE "authors" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar)
   -> 0.0039s
-- create_table(:books)
D, [2020-12-16T18:32:13.541233 #13646] DEBUG -- :    (0.1ms)  CREATE TABLE "books" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar, "total_page_count" integer, "body" text, "author_id" integer)
D, [2020-12-16T18:32:13.541413 #13646] DEBUG -- :    (0.1ms)  CREATE INDEX "index_books_on_author_id" ON "books" ("author_id")
   -> 0.0004s
D, [2020-12-16T18:32:13.565596 #13646] DEBUG -- :    (0.1ms)  CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL)
D, [2020-12-16T18:32:13.573462 #13646] DEBUG -- :   ActiveRecord::InternalMetadata Load (0.6ms)  SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ?  [["key", "environment"], ["LIMIT", 1]]
D, [2020-12-16T18:32:13.576513 #13646] DEBUG -- :   TRANSACTION (0.0ms)  begin transaction
D, [2020-12-16T18:32:13.576700 #13646] DEBUG -- :   ActiveRecord::InternalMetadata Create (0.1ms)  INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["key", "environment"], ["value", "default_env"], ["created_at", "2020-12-16 09:32:13.576211"], ["updated_at", "2020-12-16 09:32:13.576211"]]
D, [2020-12-16T18:32:13.576877 #13646] DEBUG -- :   TRANSACTION (0.0ms)  commit transaction
Run options: --seed 49138

# Running:

D, [2020-12-16T18:32:13.585338 #13646] DEBUG -- :   TRANSACTION (0.0ms)  begin transaction
D, [2020-12-16T18:32:13.585538 #13646] DEBUG -- :   Author Create (0.1ms)  INSERT INTO "authors" ("name") VALUES (?)  [["name", "naoty"]]
D, [2020-12-16T18:32:13.585665 #13646] DEBUG -- :   TRANSACTION (0.0ms)  commit transaction
D, [2020-12-16T18:32:13.598573 #13646] DEBUG -- :   TRANSACTION (0.1ms)  begin transaction
D, [2020-12-16T18:32:13.598737 #13646] DEBUG -- :   Book Create (0.1ms)  INSERT INTO "books" ("title", "total_page_count", "body", "author_id") VALUES (?, ?, ?, ?)  [["title", "dummy"], ["total_page_count", 100], ["body", "dummy"], ["author_id", 1]]
D, [2020-12-16T18:32:13.598890 #13646] DEBUG -- :   TRANSACTION (0.1ms)  commit transaction
F

Finished in 0.020147s, 49.6352 runs/s, 49.6352 assertions/s.

  1) Failure:
EagerLoadTest#test_assert [eager_load_with_selected_association.rb:46]:
Expected false to be truthy.

1 runs, 1 assertions, 1 failures, 0 errors, 0 skips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment