I'm working on optimizing some queries in my rails app (postgres), on some queries where I'm loading up a bunch of Activerecord objects and checking for any? on them, I see queries like this in my log file
SELECT
COUNT(count_column)
FROM
(SELECT
1 AS count_column
FROM
"questions"
WHERE
"questions"."deleted_at" IS NULL
AND "questions"."place_id" = 1
AND "questions"."publish_state" = 'published' LIMIT 6) subquery_for_count
I'm wondering why this is. I thought Rails would just do a count of items in memory instead of issuing a DB call to get this number?
any?on a relation object it goes to the database because that's what you asked it to do. The relation doesn't know what you have in memory, it only knows how to talk to the database. - mu is too short