Robert Head
2007-Feb-02 22:33 UTC
[Betternestedset-talk] Performance of parent, full_set, all_children, and children
I recently implemented better_nested_set in a forum project and found that every time I called parent, full_set, all_children, or children, the DB got hit again. To fix this I overrode those methods as has_many and belongs_to relationships, which are, apparently, cached by ActiveRecord. Why no caching? Is this a known limitation or am I missing something? Many thanks, Robert Head ----- Here''s my code: class Post < ActiveRecord::Base # blah, blah, blah... acts_as_nested_set :left_column => "lft", :right_column => "rgt", :scope => :topic, :text_column => ''subject'' # use faster relationships for familial relationships alias nested_set_children children has_many :children, :class_name => "Post", :order => "lft", :foreign_key => :parent_id has_many :self_and_descendants, :class_name => "Post", :order => "lft", :finder_sql => ''SELECT * FROM posts WHERE topic_id = #{topic_id} AND (lft BETWEEN #{lft} AND #{rgt})'' alias nested_set_parent parent belongs_to :parent, :class_name => "Post", :foreign_key => :parent_id # yadda, yadda, yadda... end -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/betternestedset-talk/attachments/20070202/952c35b4/attachment.html
Krishna Dole
2007-Feb-02 22:46 UTC
[Betternestedset-talk] Performance of parent, full_set, all_children, and children
Hey Robert, In a word, you''re right. The trunk version of better nested set doesn''t take this approach because the attribute setting methods of the associations would cause problems (Item.children.create(), Item.parent=). However, in the branch I''m working on (branches/ez-set) I do use the approach you are describing. If I can get a few performance concerns addressed in this branch it may become the trunk version. cheers, Krishna On 2/2/07, Robert Head <robert.head at gmail.com> wrote:> I recently implemented better_nested_set in a forum project and found that > every time I called parent, full_set, all_children, or children, the DB got > hit again. > > To fix this I overrode those methods as has_many and belongs_to > relationships, which are, apparently, cached by ActiveRecord. > > Why no caching? Is this a known limitation or am I missing something? > > Many thanks, > Robert Head > > ----- > > Here''s my code: > > class Post < ActiveRecord::Base > > # blah, blah, blah... > > acts_as_nested_set :left_column => "lft", :right_column => "rgt", :scope > => :topic, :text_column => ''subject'' > > # use faster relationships for familial relationships > > alias nested_set_children children > has_many :children, :class_name => "Post", :order => "lft", :foreign_key > => :parent_id > > has_many :self_and_descendants, :class_name => "Post", :order => "lft", > :finder_sql => ''SELECT * FROM posts WHERE topic_id = #{topic_id} AND > (lft BETWEEN #{lft} AND #{rgt})'' > > alias nested_set_parent parent > belongs_to :parent, :class_name => "Post", :foreign_key => :parent_id > > # yadda, yadda, yadda... > > end > > > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk > > >
Robert Head
2007-Feb-02 23:38 UTC
[Betternestedset-talk] Performance of parent, full_set, all_children, and children
Thanks very much for the info. It confirms my approach. And good luck with integrating the performance improvements. They sound like a good thing. Cheers, Rob On 2/2/07, Krishna Dole <dontfall at gmail.com> wrote:> > Hey Robert, > > In a word, you''re right. The trunk version of better nested set > doesn''t take this approach because the attribute setting methods of > the associations would cause problems (Item.children.create(), > Item.parent=). However, in the branch I''m working on (branches/ez-set) > I do use the approach you are describing. If I can get a few > performance concerns addressed in this branch it may become the trunk > version. > > cheers, > Krishna > > On 2/2/07, Robert Head <robert.head at gmail.com> wrote: > > I recently implemented better_nested_set in a forum project and found > that > > every time I called parent, full_set, all_children, or children, the DB > got > > hit again. > > > > To fix this I overrode those methods as has_many and belongs_to > > relationships, which are, apparently, cached by ActiveRecord. > > > > Why no caching? Is this a known limitation or am I missing something? > > > > Many thanks, > > Robert Head > > > > ----- > > > > Here''s my code: > > > > class Post < ActiveRecord::Base > > > > # blah, blah, blah... > > > > acts_as_nested_set :left_column => "lft", :right_column => "rgt", > :scope > > => :topic, :text_column => ''subject'' > > > > # use faster relationships for familial relationships > > > > alias nested_set_children children > > has_many :children, :class_name => "Post", :order => "lft", > :foreign_key > > => :parent_id > > > > has_many :self_and_descendants, :class_name => "Post", :order => > "lft", > > :finder_sql => ''SELECT * FROM posts WHERE topic_id = #{topic_id} > AND > > (lft BETWEEN #{lft} AND #{rgt})'' > > > > alias nested_set_parent parent > > belongs_to :parent, :class_name => "Post", :foreign_key => :parent_id > > > > # yadda, yadda, yadda... > > > > end > > > > > > _______________________________________________ > > Betternestedset-talk mailing list > > Betternestedset-talk at rubyforge.org > > http://rubyforge.org/mailman/listinfo/betternestedset-talk > > > > > > > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/betternestedset-talk/attachments/20070202/18402283/attachment.html