Skip to content

Instantly share code, notes, and snippets.

@mockdeep
Created August 27, 2012 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mockdeep/3492105 to your computer and use it in GitHub Desktop.
Save mockdeep/3492105 to your computer and use it in GitHub Desktop.
Benchmark.bm do|b|
b.report("exclusion join ") do
a = ""
10_000.times do
Element.find_by_sql(
<<-SQL
select e.* from elements e
left outer join elements leafs
on e.id = leafs.parent_id
where leafs.parent_id is null
SQL
)
end
end
b.report("sub query ") do
a = ""
10_000.times do
Element.find_by_sql(
<<-SQL
SELECT * FROM elements
WHERE id NOT IN (
SELECT parent_id FROM elements
WHERE parent_id IS NOT NULL
)
SQL
)
end
end
end
# results:
# user system total real
# exclusion join 2.440000 0.200000 2.640000 ( 4.545996)
# sub query 1.900000 0.180000 2.080000 ( 3.591858)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment