This is a question regarding ActiveRecord query methods:
first
Find the first record (or first N records if a parameter is supplied). If no order is defined it will order by primary key.take
Gives a record (or N records if a parameter is supplied) without any implied order. The order will depend on the database implementation. If an order is supplied it will be respected.
usecase: retrieve record from database based on unique attribute, example.
User.where(email: 'f@example.com')
here,
first
generates
SELECT "users".* FROM "users" WHERE "users"."email" = 'f@example.com' ORDER BY "users"."id"` ASC LIMIT 1
take
generates
SELECT "users".* FROM "users" WHERE "users"."email" = 'f@example.com' LIMIT 1
so as seen above first
adds additional ordering clause. I am wondering if there a performance difference between take
vs first
.
Is take
faster than first
or vice-versa?