For Rails 5+ (Ruby 2.4.1 & Postgres 9.6)
I have 100 foos
and 9900 bars
. 99 of the foos
each have 100 bars
, and one of them has none.
Foo.left_outer_joins(:bars).where(bars: { foo_id: nil })
Produces one SQL query:
Foo Load (2.3ms) SELECT "foos".* FROM "foos" LEFT OUTER JOIN "bars" ON "bars"."foo_id" = "foos"."id" WHERE "bars"."foo_id" IS NULL
and returns the one Foo
with no bars
The currently accepted answer Foo.where.not(id: Bar.select(:foo_id).uniq)
is not working. It is producing two SQL queries:
Bar Load (8.4ms) SELECT "bars"."foo_id" FROM "bars"
Foo Load (0.3ms) SELECT "foos".* FROM "foos" WHERE ("foos"."id" IS NOT NULL)
which returns all foos
because all foos
have an id
that is not null.
It needs to be changed to Foo.where.not(id: Bar.pluck(:foo_id).uniq)
to reduce it to one query and find our Foo
, but it performs poorly in benchmarks
require 'benchmark/ips'
require_relative 'config/environment'
Benchmark.ips do |bm|
bm.report('left_outer_joins') do
Foo.left_outer_joins(:bars).where(bars: { foo_id: nil })
end
bm.report('where.not') do
Foo.where.not(id: Bar.pluck(:foo_id).uniq)
end
bm.compare!
end
Warming up --------------------------------------
left_outer_joins 1.143k i/100ms
where.not 6.000 i/100ms
Calculating -------------------------------------
left_outer_joins 13.659k (± 9.0%) i/s - 68.580k in 5.071807s
where.not 70.856 (± 9.9%) i/s - 354.000 in 5.057443s
Comparison:
left_outer_joins: 13659.3 i/s
where.not: 70.9 i/s - 192.77x slower