ActiveRecords select (: id) .collect vs. pluck (: id): Why is pure AR "breaking" slower?

I am trying to get all identifiers from my article model. I can do this in two ways:

 Article.select(:id).collect{|a| a.id}
  Article Load (2.6ms)  SELECT "articles"."id" FROM "articles" 

OR

2.2.1 :006 > Article.pluck(:id)
   (4.3ms)  SELECT "articles"."id" FROM "articles"

What gives? Why is AR slower than the Ruby version?

Even when I compare the Ruby method, it looks faster:

Benchmark.measure{Article.select(:id).collect{|a| a.id}}
  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>
+4
source share
2 answers

Your rating is inaccurate. First of all, as you can see, both executions on the database side run the same query

SELECT "articles"."id" FROM "articles"

. , , , , 100 , , , , ..

, .

, Ruby . , Pluck collect, ActiveRecord, .

, ( , ), Ruby.

, pluck, , .

+4

select . ActiveRecord::Relation.

pluck , select, .

.

+1

All Articles