mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 08:43:25 +08:00
55a1394342
Doing .pluck(:column).first is a very common pattern in Discourse and in most cases, a limit cause isn't being added. Instead of adding a limit clause to all these callsites, this commit adds two new methods to ActiveRecord::Relation: pluck_first, equivalent to limit(1).pluck(*columns).first and pluck_first! which, like other finder methods, raises an exception when no record is found
29 lines
554 B
Ruby
29 lines
554 B
Ruby
# frozen_string_literal: true
|
|
|
|
require 'benchmark/ips'
|
|
require File.expand_path("../../config/environment", __FILE__)
|
|
|
|
conn = ActiveRecord::Base.connection.raw_connection
|
|
|
|
Benchmark.ips do |b|
|
|
b.report("simple") do
|
|
User.first.name
|
|
end
|
|
|
|
b.report("simple with select") do
|
|
User.select("name").first.name
|
|
end
|
|
|
|
b.report("pluck with first") do
|
|
User.pluck_first(:name)
|
|
end
|
|
|
|
b.report("pluck with limit") do
|
|
User.pluck_first(:name)
|
|
end
|
|
|
|
b.report("raw") do
|
|
conn.exec("SELECT name FROM users LIMIT 1").getvalue(0, 0)
|
|
end
|
|
end
|