Nickolay Kolev
2013-Feb-21 08:58 UTC
Using a sorted set for collecting association ids when eager loading
Consider the following gist: https://gist.github.com/nmk/5002233 Eager loading :reporter and :assignee for the bug object results in two very similar queries: User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (1, 2) User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (2, 1) This prevents AR from using its query cache and the DB from using its own caching if any were applicable. If, instead of using arrays to collect the reporter_ids and assignee_ids, we were using sorted sets the above eager loading would have resulted in the same query being issued twice: User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (1, 2) thus reusing the AR cache and (if not caught at the AR level) possibly getting help from the caching layer in the DB. Is there an explicit reason AR is using arrays here? Are we looking for lists of association objects instead of sets of association objects? Surely this would only help in a small number of cases and the performance impact of using sets instead of arrays might become an issue. I am willing to take a crack at this, unless someone chimes in with reasonable arguments against it. Best, Nickolay -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.