Ruby on rails mongoid db中的Pull vs distinct。哪个更快?
mongodb似乎有两种等效的方法: 它们都只返回集合中给定的字段 所以两者都是Ruby on rails mongoid db中的Pull vs distinct。哪个更快?,ruby-on-rails,ruby,mongodb,mongoid,Ruby On Rails,Ruby,Mongodb,Mongoid,mongodb似乎有两种等效的方法: 它们都只返回集合中给定的字段 所以两者都是 User.pluck(:name) User.distinct(:name) 将返回数据库中用户集合中所有名称的数组 > ['john', 'maria', 'tony', 'filip'] 我不介意复制品。哪种方法更快?让我们运行一个基准测试 require 'benchmark' 1_200.times { FactoryGirl.create(:user) } Benchmark.bmbm(7
User.pluck(:name)
User.distinct(:name)
将返回数据库中用户集合中所有名称的数组
> ['john', 'maria', 'tony', 'filip']
我不介意复制品。哪种方法更快?让我们运行一个基准测试
require 'benchmark'
1_200.times { FactoryGirl.create(:user) }
Benchmark.bmbm(7) do |bm|
bm.report('pluck') do
User.pluck(:email)
end
bm.report('pluck.uniq') do
User.pluck(:email).uniq
end
bm.report('only.pluck') do
User.only(:email).pluck(:email)
end
bm.report('only.pluck.uniq') do
User.only(:email).pluck(:email).uniq
end
bm.report('distinct') do
User.distinct(:email)
end
bm.report('only.distnct') do
User.only(:email).distinct(:email)
end
end
哪些产出:
Rehearsal ------------------------------------------------
pluck 0.010000 0.000000 0.010000 ( 0.009913)
pluck.uniq 0.010000 0.000000 0.010000 ( 0.012156)
only.pluck 0.000000 0.000000 0.000000 ( 0.008731)
distinct 0.000000 0.000000 0.000000 ( 0.004830)
only.distnct 0.000000 0.000000 0.000000 ( 0.005048)
--------------------------------------- total: 0.020000sec
user system total real
pluck 0.000000 0.000000 0.000000 ( 0.007904)
pluck.uniq 0.000000 0.000000 0.000000 ( 0.008440)
only.pluck 0.000000 0.000000 0.000000 ( 0.008243)
distinct 0.000000 0.000000 0.000000 ( 0.004604)
only.distnct 0.000000 0.000000 0.000000 ( 0.004510)
这清楚地表明,使用<代码>不同的比