I am trying to get all the ids from my Article model. I can do it two ways:
Article Load (2.6ms) SELECT "articles"."id" FROM "articles"
2.2.1 :006 > Article.pluck(:id)
(4.3ms) SELECT "articles"."id" FROM "articles"
Article Load (1.9ms) SELECT "articles"."id" FROM "articles"
=> #<Benchmark::Tms:0x007feb12060658 @label="", @real=0.026455502957105637, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.020000000000000018, @total=0.020000000000000018>
Your benchmark is inaccurate. First of all, as you can see, both executions on the database side triggers the same query
SELECT "articles"."id" FROM "articles"
Therefore, the database time should be considered irrelevant. Clearly the two queries had different execution time as shown by the console, but this is normal as if you run the same query 100 times the execution time can be different each time as it depends by a variety of variables such as the machine load, the database state, etc.
Since the database execution time can be considered equivalent, it's irrelevant for the benchmark.
Therefore, what you need to compare is the Ruby execution time and allocation. Pluck is supposed to be faster and more lightweight as compared to
collect it doesn't allocate ActiveRecord objects, rather it returns only the selected values.
If you really want to benchmark the methods, you should mock the database time (which is clearly variable but irrelevant for this benchmark) and only benchmark allocation and the two different Ruby methods.
Long story short,
pluck is generally more efficient.