Merb/DataM
例えば、Post.has n, :comments な関係がある時に、 以下のようなコードを実行すると、このようになります。
1 ?> Post.first.comments.count #=> 188 2 ~ (0.000865) SELECT "id" FROM "posts" ORDER BY "id" LIMIT 1 3 ~ (0.000094) SELECT "id", "post_id" FROM "comments" WHERE ("post_id" IN (1)) ORDER BY "id" 4 ~ (0.000063) SELECT COUNT(*) FROM "comments" WHERE ("post_id" = 1)
2つ目のSQLは不要なのですが、発行されてしまいます。 例えばCommentのレコード数が多かった場合、Commentオブジェクトを大量に生成しようとしてしまうため、致命的な遅さになってしまいます。
目下この問題の解決策を探しているところですが、
Edge-DMのspec/仕様でerrorが出ている状態なので、なかなか手が付けられない感じです。
とりあえず、dm-coreのassociatio
1 # @api private 2 def get_children(parent, options = {}, finder = :all, *args) 3 parent_val ue = parent_key .get(parent) 4 bind_value s = [ parent_val ue ] 5 6 with_repos itory(child_mode l) do |r| 7 parent_ide ntity_map = parent.repository .identity_m ap(parent_mod el) 8 9 query_valu es = parent_ide ntity_map. keys 10 bind_value s = query_valu es unless query_valu es.empty? 11 query = child_key. zip(bind_value s.transpose).to_hash 12 collection = child_mode l.send(finder, *(args.dup << @query.merge(optio 13 ns).merge(query))) 14 15 return collection unless collection .kind_of?(Collection ) && collection .any?
の最後の collection
posted by
genki
on Wed 8 Apr 2009
at 05:06