mercredi 27 mai 2015

Performance differences between .exists? and .where.present?

What performance differences, if any, exist between the following two options (mentioned in this answer)

Thing.where(name: "Bob").present?

which produces the SQL

SELECT COUNT(*) FROM things WHERE things.name = "Bob";

and

Thing.exists?(name: "Bob")

which produces the SQL

SELECT 1 AS one from things WHERE name ="Bob" limit 1;

As the SQL statements are different, it's theoretically possible for differences in performance to exist. But I don't know whether, assuming name is indexed in the database, there's any practical difference. Also, is there any difference with regards to the amount of work being done in Ruby-land (such as initialization and GC).

If it makes any difference, I'm using Rails 3.2.20.

Aucun commentaire:

Enregistrer un commentaire