最近开始处理现有项目,该项目严重依赖awesome_nested_set gem并使用其中一种方法self_and_descendants
引用here
此方法在拉动所有子项方面过于繁重,并触发了许多查询。我已经建立了索引,但正在寻找替代方案来解决这个性能问题。
触发的示例Sql
q = Group.first
q.self_and_descendants
以上代码触发了以下查询,还有更多只有一些被粘贴
Group::Translation Load (0.3ms) SELECT `group_translations`.* FROM `group_translations` WHERE `group_translations`.`group_id` = 2231
Group::Translation Load (0.3ms) SELECT `group_translations`.* FROM `group_translations` WHERE `group_translations`.`group_id` = 2233
Group::Translation Load (0.4ms) SELECT `group_translations`.* FROM `group_translations` WHERE `group_translations`.`group_id` = 2239
Group::Translation Load (0.3ms) SELECT `group_translations`.* FROM `group_translations` WHERE `group_translations`.`group_id` = 2240
Group::Translation Load (0.3ms) SELECT `group_translations`.* FROM `group_translations` WHERE `group_translations`.`group_id` = 2241
Group::Translation Load (0.3ms) SELECT `group_translations`.* FROM `group_translations` WHERE `group_translations`.`group_id` = 2242
Group::Translation Load (0.3ms) SELECT `group_translations`.* FROM `group_translations` WHERE `group_translations`.`group_id` = 2252
答案 0 :(得分:0)
花了一些时间后没有回复。 :)我做了这件事。
class Group < ActiveRecord::Base
acts_as_nested_set
def self_and_descendants_hashed
ActiveRecord::Base.connection.select_all("SELECT * FROM groups WHERE lft >= #{self.lft} AND rgt <= #{self.rgt} order by lft")
end
end