18

I have been using different methods to get specific fields from active record, But which one is faster and preferred to use and how are they different from one another?

User.all.collect(&:name)
User.all.pluck(:name)
User.all.select(:name)
User.all.map(&:name)

Thanks for your help in advance.

2
  • When you measured that, what results did you get? Also, not all of these methods get you an array of that specific field. Jul 27, 2017 at 9:57
  • @SergioTulentsev the run time was same 0.1ms and select gave the result in this format [#<User id: nil, name: "Tom">, #<User id: nil, name: "Jerry">] rest of them gave in this format ["Tom", "Jerry"]
    – Touqeer
    Jul 27, 2017 at 10:30

2 Answers 2

16

Usage of any of these methods requires different use cases:

Both select and pluck make SQL's SELECT of specified columns (SELECT "users"."name" FROM "users"). Hence, if you don't have users already fetched and not going to, these methods will be more performant than map/collect.

The difference between select and pluck:

  • Performance: negligible when using on a reasonable number of records
  • Usage: select returns the list of models with the column specified, pluck returns the list of values of the column specified. Thus, again, the choice depends on the use case.

collect/map methods are actually aliases, so there's no difference between them. But to iterate over models they fetch the whole model (not the specific column), they make SELECT "users".* FROM "users" request, convert the relation to an array and map over it.

This might be useful, when the relation has already been fetched. If so, it won't make additional requests, what may end up more performant than using pluck or select. But, again, must be measured for a specific use case.

3
  • 3
    "Performance: negligible" - the query is the same, indeed. But instantiating thousands of AR objects is far from free. All of these need to measured in the OP's environment. We can only wave hands and make wild guesses here :) Jul 27, 2017 at 10:17
  • @Igor thanks for the reply. Those are really good information you provided.
    – Touqeer
    Jul 27, 2017 at 10:33
  • 1
    @SergioTulentsev yes, the diff in performance between select and pluck will be the more noticeable the greater the number of records. I just wanted to point out that the choice depends on the current state. Sometimes it's allowable to choose a particular method in order to improve readability, even if we lose the milliseconds in performance :) Jul 27, 2017 at 10:39
7

pluck: retrieve just names from users, put them in an array as strings (in this case) and give it to you.

select: retrieve all the users from db with just the 'name' column and returns a relation.

collect/map (alias): retrieve all the users from db with all columns, put them in an array of User objects with all the fields, then transform every object in just the name and give this names array to you.

I put this in order of performance to me.

EDIT after some years: now pluck is even present as a method for enumerable, not only for active record relations and it works in the same way as map

10
  • If, by your description, pluck and select do the same thing, why is pluck faster? Jul 27, 2017 at 10:03
  • Nope, pluck doesn't make User objects, it returns an array of strings in this case
    – Ursus
    Jul 27, 2017 at 10:04
  • Yes, and it also doesn't retrieve user records from database. Just their names. Big difference. Jul 27, 2017 at 10:04
  • Well ok, you retrieve names but from users. This is just terminology to me.
    – Ursus
    Jul 27, 2017 at 10:07
  • 1
    Bon appetit :) Nah, I'm just nitpicking out of boredom :) Jul 27, 2017 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.