本文介绍了在蒙古语数据库中采摘vs截然不同.哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
似乎mongodb有两个等效的方法:
It seems that mongodb has two equivalent methods:
#pluck 和#distinct 都只返回集合中给定的字段.
#pluck and #distinct which both return only given fields from a collection.
所以都
User.pluck(:name)
User.distinct(:name)
将返回db中User集合中所有名称的数组
would return array of all names from User collection in db
> ['john', 'maria', 'tony', 'filip']
我不介意重复.哪种方法更快?
I don't mind duplicates. Which method is faster?
推荐答案
让我们运行基准测试!
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)
它清楚地表明,使用#distinct
的速度几乎是#pluck
it clearly shows that using #distinct
is almost two times faster than #pluck
这篇关于在蒙古语数据库中采摘vs截然不同.哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!