Create cop to enforce exists? over any? and present? in ActiveRecord relations
Description of the proposal
This is the result of executing any?, exists? and present?:
User.where.not(preferred_language: 'en').present? => SELECT "users".* FROM "users" WHERE ("users"."preferred_language" != 'en') # Existence retrieved from the loaded records' array
User.where.not(preferred_language: 'en').any? => SELECT COUNT(*) FROM "users" WHERE ("users"."preferred_language" = 'en')
User.where.not(preferred_language: 'en').exists? => SELECT 1 AS one FROM "users" WHERE ("users"."preferred_language" != 'en') LIMIT 1
As you can see, exists? is faster because the query stops when it gets the first result.
# GOOD
User.where(preferred_language: 'en').exists?
User.exists?(preferred_language: 'en') # Even better
# BAD
User.where(preferred_language: 'en').any?
User.where(preferred_language: 'en').present?
-
https://www.ombulabs.com/blog/benchmark/performance/rails/present-vs-any-vs-exists.html
-
Mention the proposal in the next backend weekly call and the #backend channel to encourage contribution -
Proceed with the proposal once 50% of the maintainers have weighed in, and 80% of the votes are 👍 -
Once approved, mention it again in the next backend weekly call and the #backend channel
Edited by 🤖 GitLab Bot 🤖