1

我试图避免急切加载的 N+1 查询问题,但它不起作用。相关模型仍在单独加载。

以下是相关的 ActiveRecords 及其关系:

class Player < ActiveRecord::Base
  has_one :tableau
end

Class Tableau < ActiveRecord::Base
  belongs_to :player
  has_many :tableau_cards
  has_many :deck_cards, :through => :tableau_cards
end

Class TableauCard < ActiveRecord::Base
  belongs_to :tableau
  belongs_to :deck_card, :include => :card
end

class DeckCard < ActiveRecord::Base
  belongs_to :card
  has_many :tableaus, :through => :tableau_cards
end

class Card < ActiveRecord::Base
  has_many :deck_cards
end

class Turn < ActiveRecord::Base
  belongs_to :game
end

我正在使用的查询在 Player 的这个方法中:

def tableau_contains(card_id)
  self.tableau.tableau_cards = TableauCard.find :all, :include => [ {:deck_card => (:card)}], :conditions => ['tableau_cards.tableau_id = ?', self.tableau.id]
  contains = false
  for tableau_card in self.tableau.tableau_cards
    # my logic here, looking at attributes of the Card model, with        
    # tableau_card.deck_card.card;
    # individual loads of related Card models related to tableau_card are done here
  end
  return contains
end

它与范围有关吗?这个 tableau_contains 方法在一个更大的循环中减少了几个方法调用,我最初尝试进行急切加载,因为有几个地方循环和检查这些相同的对象。然后我最终尝试了上面的代码,在循环之前加载,我仍然在日志中的 tableau_cards 循环中看到 Card 的单个 SELECT 查询。我也可以在 tableau_cards 循环之前看到带有 IN 子句的急切加载查询。

编辑:下面有更大的外循环的附加信息

EDIT2:用答案中的提示纠正了下面的循环

EDIT3:在目标循环中添加了更多细节

这是更大的循环。它在 after_save 的观察者内

def after_save(pa)
  turn = Turn.find(pa.turn_id, :include => :player_actions)
  game = Game.find(turn.game_id, :include => :goals)
  game.players.all(:include => [ :player_goals, {:tableau => [:tableau_cards => [:deck_card => [:card]]]} ])
  if turn.phase_complete(pa, players)  # calls player.tableau_contains(card)
    for goal in game.goals
      if goal.checks_on_this_phase(pa)
        if goal.is_available(players, pa, turn)
          for player in game.players
            goal.check_if_player_takes(player, turn, pa)
              ... # loop through player.tableau_cards
            end
          end
        end
      end
    end
  end

这是turn类中的相关代码:

def phase_complete(phase, players)
  all_players_complete = true
  for player in players
    if(!player_completed_phase(player, phase))
      all_players_complete = false
    end
  end
  return all_players_complete
end

正在for player in game.players执行另一个查询以加载播放器。它被缓存了,我的意思是它在日志中有 CACHE 标签,但我认为根本不会有任何查询,因为 game.players 应该已经加载到内存中。

目标模型的另一个片段:

class Goal < ActiveRecord::Base
  has_many :game_goals
  has_many :games, :through => :game_goals
  has_many :player_goals
  has_many :players, :through => :player_goals

  def check_if_player_takes(player, turn, phase)
    ...
    for tab_card in player.tableau_cards
    ...
  end
end
4

3 回答 3

6

尝试这个:

class Game
  has_many :players
end

改逻辑tableau_contains如下:

class Player < ActiveRecord::Base
  has_one :tableau
  belongs_to :game

  def tableau_contains(card_id)
    tableau.tableau_cards.any?{|tc| tc.deck_card.card.id == card_id}
  end

end

改逻辑after_save如下:

def after_save(turn)
  game = Game.find(turn.game_id, :include => :goals))
  Rails.logger.info("Begin  eager loading..")                
  players = game.players.all(:include => [:player_goals,
            {:tableau => [:tableau_cards=> [:deck_card => [:card]]]} ])
  Rails.logger.info("End  eager loading..")                
  Rails.logger.info("Begin  tableau_contains check..")                
  if players.any?{|player| player.tableau_contains(turn.card_id)}
    # do something..                
  end
  Rails.logger.info("End  tableau_contains check..")                
end

该方法的第二行after_save急切地加载了执行tableau_contains检查所需的数据。诸如tableau.tableau_cardstc.deck_card.card应该/不会访问数据库之类的调用。

您的代码中的问题:

1)将数组分配给has_many关联

@game.players = Player.find :all, :include => ...

上面的语句不是一个简单的赋值语句。它使用给定游戏的更改palyers表格行。game_id我假设这不是你想要的。如果您检查 DB 表,您会注意到updated_time玩家表的行在分配后发生了变化。

您必须将值分配给单独的变量,如after_save方法中的代码示例所示。

2)手工编码关联SQL

在代码中的许多地方,您都在为关联数据手动编写 SQL。Rails 为此提供了关联。

例如:

tcards= TableauCard.find :all, :include => [ {:deck_card => (:card)}], 
         :conditions => ['tableau_cards.tableau_id = ?', self.tableau.id]

可以改写为:

tcards = tableau.tableau_cards.all(:include => [ {:deck_card => (:card)}])

模型上的tableau_cards卡片关联Tableau构造了您手动编码的相同 SQL。

has_many :through您可以通过向类添加关联来进一步改进上述语句Player

class Player
  has_one :tableau
  has_many :tableau_cards, :through => :tableau
end

tcards = tableau_cards.all(:include => [ {:deck_card => (:card)}])

编辑 1

我创建了一个应用程序来测试这段代码。它按预期工作。Rails 运行几个 SQL 来预先加载数据,即:

Begin  eager loading..
SELECT * FROM `players` WHERE (`players`.game_id = 1) 
SELECT `tableau`.* FROM `tableau` WHERE (`tableau`.player_id IN (1,2))
SELECT `tableau_cards`.* FROM `tableau_cards` 
          WHERE (`tableau_cards`.tableau_id IN (1,2))
SELECT * FROM `deck_cards` WHERE (`deck_cards`.`id` IN (6,7,8,1,2,3,4,5))
SELECT * FROM `cards` WHERE (`cards`.`id` IN (6,7,8,1,2,3,4,5))
End  eager loading..
Begin  tableau_contains check..
End  tableau_contains check..

在急切加载数据后,我没有看到任何 SQL 执行。

编辑 2

对您的代码进行以下更改。

def after_save(pa)
  turn = Turn.find(pa.turn_id, :include => :player_actions)
  game = Game.find(turn.game_id, :include => :goals)
  players = game.players.all(:include => [ :player_goals, {:tableau => [:tableau_cards => [:deck_card => [:card]]]} ])
  if turn.phase_complete(pa, game, players)
    for player in game.players
      if(player.tableau_contains(card))
      ...
      end
    end
  end
end
def phase_complete(phase, game, players)
  all_players_complete = true
  for player in players
    if(!player_completed_phase(player, phase))
      all_players_complete = false
    end
  end
  return all_players_complete
end

缓存的工作原理如下:

game.players # cached in the game object
game.players.all # not cached in the game object

players = game.players.all(:include => [:player_goals])
players.first.player_goals # cached

上面的第二个语句导致自定义关联查询。因此 AR 不会缓存结果。当使用标准关联 SQL 获取第三条语句中的每个玩家对象时,其中 asplayer_goals被缓存。

于 2010-05-03T05:41:25.890 回答
1

第一个问题是:您每次都在重置 player.tableau.tableau_cards

player.tableau.tableau_cards = TableauCard.find :all, :include => [ {:deck_card => (:card)}], :conditions => ['tableau_cards.tableau_id = ?', player.tableau.id] 

如果这应该是一个临时数组,那么你做的工作比必要的多。以下会更好:

temp_tableau_cards = TableauCard.find :all, :include => [ {:deck_card => (:card)}], :conditions => ['tableau_cards.tableau_id = ?', player.tableau.id] 

如果您实际上是在尝试设置 tableau_cards 并对它们做一些事情,我也会将这两个操作分开。

player.tableau.tableau_cards = TableauCard.find :all, :include => [ {:deck_card => (:card)}], :conditions => ['tableau_cards.tableau_id = ?', player.tableau.id] 
card.whatever_logic if player.tableau.tableau_cards.include? card

同样,当您不需要时,您似乎正在加倍查询。

于 2010-04-29T18:45:58.217 回答
1

如果您将cards = TableauCard.find...呼叫与player.tableau.tableau_cards = cards呼叫分开会发生什么?也许 rails 正在代码中的那一点重置关联的缓存记录,然后重新加载关联。

tableau_contains这也将允许您通过显式传入变量来确保传入相同的数组。

您似乎正试图在对关联的多次调用中保留预先加载的player.cards.tableau_cards关联。我不确定 Rails 的工作方式是否可以实现此功能。我相信它会缓存从 sql 语句返回的原始数据,而不是返回的实际数组。所以:

  def test_association_identity
   a = player.tableau.tableau_cards.all(
          :include => {:deck_card => :card}) 
          #=> Array with object_id 12345
          # and all the eager loaded deck and card associations set up
   b = player.tableau.tableau_cards 
          #=> Array 320984230 with no eager loaded associations set up. 
          #But no extra sql query since it should be cached.
   assert_equal a.object_id, b.object_id #probably fails 
   a.each{|card| card.deck_card.card}
   puts("shouldn't have fired any sql queries, 
         unless the b call reloaded the association magically.")
   b.each{|card| card.deck_card.card; puts("should fire a query 
                                        for each deck_card and card")}
  end

我能想到的唯一另一件事是在整个代码中分散一些输出,并准确查看延迟加载发生的位置。

这就是我的意思:

#观察者

def after_save(pa)
  @game = Game.find(turn.game_id, :include => :goals)
  @game.players = Player.find( :all, 
                :include => [ {:tableau => (:tableau_cards)},:player_goals ], 
                :conditions => ['players.game_id =?', @game.id]
  for player in @game.players
    cards = TableauCard.find( :all, 
          :include =>{:deck_card => :card}, 
          :conditions => ['tableau_cards.tableau_id = ?', player.tableau.id])
    logger.error("First load")
    player.tableau.tableau_cards =  cards #See above comments as well.
    # Both sides of this ^ line should always be == since: 
    # Given player.tableau => Tableau(n) then Tableau(n).tableau_cards 
    # will all have tableau_id == n. In other words, if there are 
    # `tableau_cards.`tableau_id = n in the db (as in the find call),
    # then they'll already be found in the tableau.tableau_cards call.
    logger.error("Any second loads?")
    if(tableau_contains(cards,card))
       logger.error("There certainly shouldn't be any loads here.") 
       #so that we're not relying on any additional association calls, 
       #this should at least remove one point of confusion.
    ...
    end
  end
end

#Also in the Observer, for just these purposes (it can be moved back out 
#to Player after the subject problem here is understood better)

def tableau_contains(cards,card_id)
  contains = false
          logger.error("Is this for loop loading the cards?")
  for card in cards
           logger.error("Are they being loaded after `card` is set?")
    # my logic here, looking at attributes of the Card model, with        
    # card.deck_card.card;
    logger.error("What about prior to this call?")
  end
  return contains
end
于 2010-04-30T20:01:31.747 回答