present?
empty?
any?
exists?
Build.where(:created_at => 7.days.ago..1.day.ago).passed.present?
SELECT "builds".* FROM "builds" WHERE ("builds"."created_at" BETWEEN
2017-02-22 21:22:27.133402' AND '2017-02-28 21:22:27.133529') AND
"builds"."result" = $1 [["result", "passed"]]
Build.where(:created_at => 7.days.ago..1.day.ago).passed.any?
Build.where(:created_at => 7.days.ago..1.day.ago).passed.empty? SELECT COUNT(*) FROM "builds" WHERE ("builds"."created_at" BETWEEN '2017-02-22 21:22:16.885942' AND '2017-02-28 21:22:16.886077') AND "builds"."result" = $1 [["result", "passed"]]
Build.where(:created_at => 7.days.ago..1.day.ago).passed.exists?
SELECT 1 AS one FROM "builds" WHERE ("builds"."created_at" BETWEEN
'2017-02-22 21:23:04.066301' AND '2017-02-28 21:23:04.066443') AND
"builds"."result" = $1 LIMIT 1 [["result", "passed"]]
SELECT 1...LIMIT 1
present? => 2892.7 ms
any? => 400.9 ms
empty? => 403.9 ms
exists? => 1.1 ms
Soms 400 keer sneller
200ms is acceptabel
project = Project.find_by_name('ivaldi')
project.builds.load
project.builds.any? # no database hit
project.builds.exists? # hits the database
add_index :projects, :name
WHERE, HAVING, ORDER BY
Bijvoorbeeld:
Project.find_by(name: 'Ivaldi')
Benchmark.ms { Project.find_by_name('Tremayne Cummerata') }
# 0.5210000090301037
Benchmark.ms { Company.find_by_name('Mrs. Elvie Thompson') }
# 0.9719999507069588
# Maakt geen verschil
add_index :projects, :owner_id
add_index :projects, :owner_type
# Wordt wel sneller
add_index :projects, [:owner_id, :owner_type]
Project.order(:name)
add_index :name
Benchmark.ms { Project.order(:name) }
# 0.07900013588368893
Benchmark.ms { Company.order(:name) }
# 0.10000006295740604