In ActiveRecord, "exists?" is faster than "count > 0".

exists? generates sql like SELECT 1 AS ONE WHERE ... LIMIT 1, which means the DB can stop as soon as it finds one row.

count generates a COUNT query, which means every row must be checked and summed. Much slower!

1
Share
Share on Mastodon
Share on Twitter
Share on Facebook
Share on Linkedin
ingemar

@nateberkopec yes! I used sed it today in favor of “.any?”

0
1y
Replies