What's the difference between what appears as Load and what doesn't appear in the log when accessing DB?

Asked 1 years ago, Updated 1 years ago, 74 views

For example, if you run all, the character User Load appears.

User.all
 User Load (3.5ms) SELECT "users".* FROM "users"

On the other hand, when you run pluck, SQL itself seems to be the same, but User Load is not displayed.

User.pluck(:name)
(1.9ms) SELECT "users". "name" FROM "users"

At first, I thought it was the difference between loading it into memory, but
I think loading pluck into memory is the same, so I wondered what the difference was.

 names=User.pluck(:name)
(1.9ms) SELECT "users". "name" FROM "users"

names 
=>["foo", "bar" ]

If anyone knows the difference between XXX Load and as shown above, could you please let me know?

ruby-on-rails rails-activerecord

2022-09-30 17:52

1 Answers

The difference between whether the User object is created or not as a result of the query.(I'm sure)

Rails is based on using model objects, but #pluck does not create model objects, so it is fast when you simply want a value.


2022-09-30 17:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.