0

Recently started working on a existing project which heavily relies on awesome_nested_set gem and used one of it's method's self_and_descendants refer here

This method is too heavy in terms of pulling all the children and triggers many queries. I have indexing in place but looking for a alternative to come out of this performance issue.

Sample Sqls that are triggered

q = Group.first
q.self_and_descendants

The above code triggers below queries, there are many more only some are pasted

  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
4

1 回答 1

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
于 2013-06-28T10:47:18.770 回答