1

我已经开始通过终端注意到我的 Rails 应用程序产生了太多的 SQL 语句,其中许多不是必需的。我确实记得在某处读过这可能是一个问题,现在随着我的数据增长,它明显减慢了应用程序的速度。

例如,我有包含曲目的版本。艺术家可以被分配到轨道和版本。当我加载一个只有 8 个轨道的版本时,它似乎正在运行数据库中的每个轨道以找到这些关系!?!

例如,请参见下文,这是一个非常小的示例,但这些曲目实际上都没有与发布相关联。它通过数据库中的每一个轨道!

任何通用指针?

Artist Load (0.6ms)  SELECT `artists`.* FROM `artists` INNER JOIN `artists_tracks` ON `artists`.`id` = `artists_tracks`.`artist_id` WHERE `artists_tracks`.`track_id` = 12
  Artist Load (0.6ms)  SELECT `artists`.* FROM `artists` INNER JOIN `artists_tracks` ON `artists`.`id` = `artists_tracks`.`artist_id` WHERE `artists_tracks`.`track_id` = 19
  Artist Load (0.6ms)  SELECT `artists`.* FROM `artists` INNER JOIN `artists_tracks` ON `artists`.`id` = `artists_tracks`.`artist_id` WHERE `artists_tracks`.`track_id` = 21
  Artist Load (0.9ms)  SELECT `artists`.* FROM `artists` INNER JOIN `artists_tracks` ON `artists`.`id` = `artists_tracks`.`artist_id` WHERE `artists_tracks`.`track_id` = 22
  Artist Load (0.7ms)  SELECT `artists`.* FROM `artists` INNER JOIN `artists_tracks` ON `artists`.`id` = `artists_tracks`.`artist_id` WHERE `artists_tracks`.`track_id` = 23
  Artist Load (0.6ms)  SELECT `artists`.* FROM `artists` INNER JOIN `artists_tracks` ON `artists`.`id` = `artists_tracks`.`artist_id` WHERE `artists_tracks`.`track_id` = 24
  Artist Load (0.9ms)  SELECT `artists`.* FROM `artists` INNER JOIN `artists_tracks` ON `artists`.`id` = `artists_tracks`.`artist_id` WHERE `artists_tracks`.`track_id` = 25
  Artist Load (1.0ms)  SELECT `artists`.* FROM `artists` INNER JOIN `artists_tracks` ON `artists`.`id` = `artists_tracks`.`artist_id` WHERE `artists_tracks`.`track_id` = 26
  Artist Load (0.6ms)  SELECT `artists`.* FROM `artists` INNER JOIN `artists_tracks` ON `artists`.`id` = `artists_tracks`.`artist_id` WHERE `artists_tracks`.`track_id` = 27
  Artist Load (0.9ms)  SELECT `artists`.* FROM `artists` INNER JOIN `artists_tracks` ON `artists`.`id` = `artists_tracks`.`artist_id` WHERE `artists_tracks`.`track_id` = 28
  Artist Load (0.6ms)  SELECT `artists`.* FROM `artists` INNER JOIN `artists_tracks` ON `artists`.`id` = `artists_tracks`.`artist_id` WHERE `artists_tracks`.`track_id` = 29
  Artist Load (0.6ms)  SELECT `artists`.* FROM `artists` INNER JOIN `artists_tracks` ON `artists`.`id` = `artists_tracks`.`artist_id` WHERE `artists_tracks`.`track_id` = 30
  Artist Load (0.6ms)  SELECT `artists`.* FROM `artists` INNER JOIN `artists_tracks` ON `artists`.`id` = `artists_tracks`.`artist_id` WHERE `artists_tracks`.`track_id` = 31

以下是涉及的模型:

class Artist < ActiveRecord::Base  
  has_many :artist_releases
  has_many :releases, :through => :artist_releases 
  has_many :artists_tracks
  has_many :tracks, :through => :artists_tracks
end

class ArtistRelease < ActiveRecord::Base
  belongs_to :artist
  belongs_to :release
end

class ArtistsTrack < ActiveRecord::Base
  belongs_to :artist
  belongs_to :release
  belongs_to :track
end

class Release < ActiveRecord::Base
  has_many :artist_releases
  has_many :artists, :through => :artist_releases
  accepts_nested_attributes_for :artists, :reject_if => lambda { |a| a[:name].blank? }
  accepts_nested_attributes_for :artist_releases    
  has_many :releases_tracks, :dependent => :destroy
  has_many :tracks, :through => :releases_tracks, :order => "releases_tracks.position"
  accepts_nested_attributes_for :tracks, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => :true
  accepts_nested_attributes_for :releases_tracks    
end

class ReleasesTrack < ActiveRecord::Base
  default_scope :order => 'releases_tracks.position ASC'
  acts_as_list :scope => :release_id
  belongs_to :release
  belongs_to :track
end

class Track < ActiveRecord::Base
  has_many :releases_tracks, :dependent => :destroy
  has_many :releases, :through => :releases_tracks
  has_many :artists_tracks
  has_many :artists, :through => :artists_tracks
  accepts_nested_attributes_for :artists, :reject_if => lambda { |a| a[:name].blank? }
  accepts_nested_attributes_for :artists_tracks
end
4

1 回答 1

5

从 Rails 生成的查询来看,您似乎正在做这样的事情,当您引用它时,它会单独加载每个艺术家:

release.tracks.each{ |t| t.artist }

您应该急切地加载艺术家,以便在单个查询中预先加载它们:

release.tracks.includes(:artist).each{ |t| t.artist }

includes方法非常灵活,并且允许您急切地加载多个关联,甚至是嵌套关联:

Release.first.includes(:releases_tracks => {:track => :artist})

这将在 4 个查询(每个表 1 个)中加载第一个版本,然后是它的所有 ReleaseTrack,然后是它们的所有 Track,以及所有 Track 的 Artists。这比单独加载每条记录要高效得多。

Active Record Query interface guide 中的 Eager Loading Associations部分提供了更多信息。

于 2012-05-31T16:32:36.803 回答