تخطَّ إلى المحتوى

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.

if Order.where(user_id: id).count > 0
notify(id)
end
if Order.where(user_id: id).exists?
notify(id)
end

Use the short-circuiting exists? for presence checks instead of count > 0.