optimize pluck

This commit is contained in:
Dylan Knutson
2025-07-29 05:43:32 +00:00
parent ba1b74022a
commit 3704239a6c
3 changed files with 56 additions and 37 deletions

View File

@@ -6,12 +6,16 @@ require "spec_helper"
RSpec.describe "loading optimizations" do
context "cars table" do
before do
Car.create!(name: "Toyota Camry", fuel_type: "gasoline")
Car.create!(name: "Toyota Prius", fuel_type: "hybrid")
Car.create!(name: "Toyota Corolla", fuel_type: "electric")
Car.create!(name: "Toyota Camry", fuel_type: "gasoline", engine_size: 2.0)
Car.create!(name: "Toyota Prius", fuel_type: "hybrid", engine_size: 1.5)
Car.create!(
name: "Toyota Corolla",
fuel_type: "electric",
engine_size: 1.8
)
end
it "queries only the aux table if plucking values that are on the aux table" do
it "queries only the aux table if no main table columns are referenced" do
queries =
SpecHelper.capture_queries do
expect(Car.pluck(:fuel_type)).to eq(%w[gasoline hybrid electric])
@@ -21,6 +25,21 @@ RSpec.describe "loading optimizations" do
expect(queries.first).not_to include("JOIN")
end
it "queries only the aux table if all columns are on the aux table" do
queries =
SpecHelper.capture_queries do
expect(Car.where(engine_size: 1.4..1.9).pluck(:fuel_type)).to eq(
%w[hybrid electric]
)
end
expect(queries.length).to eq(1)
expect(queries.first).not_to include("JOIN")
expect(queries.first).to include("BETWEEN")
expect(queries.first).to match(/\bvehicles_car_aux\b/)
expect(queries.first).not_to match(/\bvehicles\b/)
end
it "queries both tables if main table column is referenced" do
queries =
SpecHelper.capture_queries do