7

私はRuby 用のオープンソース ORM であるDataMapperを使用しています。現時点では、DataMapper は 1 対多の関係に Strategic Eager Loading (SEL) を使用できますが、N+1 クエリが発生する多対多には使用できません。これを正しく機能させるためにハックしたいのですが、それを行う場所が見つかりません。したがって、2つの部分からなる質問:

  1. テストスイートを実行して、これが失敗していることを示すにはどうすればよいですか (nb. 現在、失敗するはずのすべての仕様が保留中としてマークされています)。
  2. 1 対多のリレーションシップの SEL はどこでどのように実装されますか?
4

1 に答える 1

0

2 番目の質問については、コードに飛び込んでみることができます。

/lib/dm-core/associations/relationship.rb

  # Eager load the collection using the source as a base
  #
  # @param [Collection] source
  #   the source collection to query with
  # @param [Query, Hash] query
  #   optional query to restrict the collection
  #
  # @return [Collection]
  #   the loaded collection for the source
  #
  # @api private
  def eager_load(source, query = nil)
    targets = source.model.all(query_for(source, query))

    # FIXME: cannot associate targets to m:m collection yet
    if source.loaded? && !source.kind_of?(ManyToMany::Collection)
      associate_targets(source, targets)
    end

    targets
  end

./lib/dm-core/associations/one_to_many.rb:

    def lazy_load(source)
      return if loaded?(source)

      # SEL: load all related resources in the source collection
      if source.saved? && (collection = source.collection).size > 1
        eager_load(collection)
      end

      unless loaded?(source)
        set!(source, collection_for(source))
      end
    end
于 2012-03-06T08:13:47.310 に答える