Count Vs Exists (ARCH6063)
Key ruby.orm.count-vs-exists · Severity info · Category orm
Testing row presence with count > 0 forces the database to count all rows. exists? is far cheaper because it can stop at the first row.
Incorrect
Section titled “Incorrect”if Order.where(user_id: id).count > 0 notify(id)endCorrect
Section titled “Correct”if Order.where(user_id: id).exists? notify(id)endUse the short-circuiting exists? for presence checks instead of count > 0.