parm530
6/11/2019 - 4:31 PM

Count vs Length

When to use AR count and length

q = Item.where(qid: 64).count
# SELECT COUNT(*) FROM "items" WHERE "items"."qid" = $1  [["qid", 64]]

q = Item.where(qid: 64).length
SELECT "items".* FROM "items" WHERE "items"."qid" = $1  [["qid", 64]]
  • length takes all columns just to return a number
  • count doesn't take the columns, uses SQL function to return a number (might tale longer but less overhead)