Acts_as_tree leads to high CPU occupied?

ActiveRecord:

class Classify < ActiveRecord::Base
acts_as_tree :order=>“name”
end

template:

in the database table: classifies,there are about 200 records,which has
about 20 subtypes and 10 thirdtypes for each subtype on average

When i use the template above,it will occupy 100% of cpu for about 10
seconds,therefore the browser holds until the template is
rendered,following is log:

e[4;35;1mClassify Load (0.016000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id IS NULL) e[0m
e[4;36;1mClassify Columns (0.016000)e[0m e[0;1mSHOW FIELDS FROM
classifiese[0m
e[4;35;1mClassify Load (0.000000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id = ‘1’) e[0m
e[4;36;1mClassify Load (0.000000)e[0m e[0;1mSELECT * FROM classifies
WHERE (parent_id = ‘2’) e[0m
e[4;35;1mClassify Load (0.000000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id = ‘3’) e[0m
e[4;36;1mClassify Load (0.000000)e[0m e[0;1mSELECT * FROM classifies
WHERE (parent_id = ‘4’) e[0m
e[4;35;1mClassify Load (0.000000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id = ‘5’) e[0m
e[4;36;1mClassify Load (0.000000)e[0m e[0;1mSELECT * FROM classifies
WHERE (parent_id = ‘6’) e[0m
e[4;35;1mClassify Load (0.016000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id = ‘7’) e[0m
e[4;36;1mClassify Load (0.000000)e[0m e[0;1mSELECT * FROM classifies
WHERE (parent_id = ‘8’) e[0m
e[4;35;1mClassify Load (0.000000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id = ‘9’) e[0m
e[4;36;1mClassify Load (0.000000)e[0m e[0;1mSELECT * FROM classifies
WHERE (parent_id = ‘10’) e[0m
e[4;35;1mClassify Load (0.016000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id = ‘11’) e[0m
e[4;36;1mClassify Load (0.000000)e[0m e[0;1mSELECT * FROM classifies
WHERE (parent_id = ‘12’) e[0m
e[4;35;1mClassify Load (0.000000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id = ‘13’) e[0m
e[4;36;1mClassify Load (0.000000)e[0m e[0;1mSELECT * FROM classifies
WHERE (parent_id = ‘14’) e[0m
e[4;35;1mClassify Load (0.000000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id = ‘15’) e[0m
e[4;36;1mClassify Load (0.000000)e[0m e[0;1mSELECT * FROM classifies
WHERE (parent_id = ‘16’) e[0m
e[4;35;1mClassify Load (0.000000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id = ‘17’) e[0m
e[4;36;1mClassify Load (0.000000)e[0m e[0;1mSELECT * FROM classifies
WHERE (parent_id = ‘18’) e[0m
e[4;35;1mClassify Load (0.000000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id = ‘19’) e[0m
e[4;36;1mClassify Load (0.000000)e[0m e[0;1mSELECT * FROM classifies
WHERE (parent_id = ‘20’) e[0m
e[4;35;1mClassify Load (0.000000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id = ‘21’) e[0m
e[4;36;1mClassify Load (0.000000)e[0m e[0;1mSELECT * FROM classifies
WHERE (parent_id = ‘22’) e[0m
e[4;35;1mClassify Load (0.000000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id = ‘23’) e[0m
e[4;36;1mClassify Load (0.015000)e[0m e[0;1mSELECT * FROM classifies
WHERE (parent_id = ‘24’) e[0m
e[4;35;1mClassify Load (0.000000)e[0m e[0mSELECT * FROM classifies
WHERE (parent_id = ‘25’) e[0m
Rendered classifies/_index (12.29700)

If All the SQL are executed in MYSQL,it needs 0.03 seconds only,but the
whole render time is terrible 12 seconds!

I have not found some better way to resolve it,Can anyone here provide
some way to avoid of the high CPU occupy?
btw: According to the chat with Zed S. yesterday,Shaw thinks it is
because of the GC start of ruby language. Since i think acts_as_tree is
a very usual application,as to such an application, Others who has used
it have not found such a phenomena?

Best Regards

Andy

Andy wrote:

ActiveRecord:

class Classify < ActiveRecord::Base
acts_as_tree :order=>“name”
end

template:

in the database table: classifies,there are about 200 records,which has
about 20 subtypes and 10 thirdtypes for each subtype on average

When i use the template above,it will occupy 100% of cpu for about 10
seconds,therefore the browser holds until the template is
rendered,following is log:

Rendered classifies/_index (12.29700)

If All the SQL are executed in MYSQL,it needs 0.03 seconds only,but the
whole render time is terrible 12 seconds!

I have not found some better way to resolve it,Can anyone here provide
some way to avoid of the high CPU occupy?
btw: According to the chat with Zed S. yesterday,Shaw thinks it is
because of the GC start of ruby language. Since i think acts_as_tree is
a very usual application,as to such an application, Others who has used
it have not found such a phenomena?

This is a great place to use acts_as_nested_set, which lets you fetch an
item and all its subtree contents in one query. Once fetched, you can
walk the tree in Ruby as you please.


Josh S.
http://blog.hasmanythrough.com

I believe acts_as_nested_set can get the entire tree with one SQL
statement.

Andy wrote:

Josh S. wrote:

This is a great place to use acts_as_nested_set, which lets you fetch an
item and all its subtree contents in one query. Once fetched, you can
walk the tree in Ruby as you please.

I have found the method of all_children of acts_as_nested_set can get
all the nested children of the root,but i want to display the tree
structure in the page. Therefore direct_children has to be used:

root.direct_children.each do|subtype|
subtype.direct_children.each do|thirdtype|

end
end

This is the same as the original…

Once you have the items fetched into memory as model objects, you can
fake up the hierarchy by indenting based on the depth in the tree. Bob
Silva has a nice writeup on how to do it:
http://www.railtie.net/articles/2006/03/31/implement-acts_as_threaded-without-a-plugin


Josh S.
http://blog.hasmanythrough.com

Josh S. wrote:

This is a great place to use acts_as_nested_set, which lets you fetch an
item and all its subtree contents in one query. Once fetched, you can
walk the tree in Ruby as you please.


Josh S.
http://blog.hasmanythrough.com

I have found the method of all_children of acts_as_nested_set can get
all the nested children of the root,but i want to display the tree
structure in the page. Therefore direct_children has to be used:

root.direct_children.each do|subtype|
subtype.direct_children.each do|thirdtype|

end
end

This is the same as the original…

Hi,

Le 22 août 06, à 03:06, Andy a écrit :

I have not found some better way to resolve it,Can anyone here provide
some way to avoid of the high CPU occupy?
btw: According to the chat with Zed S. yesterday,Shaw thinks it is
because of the GC start of ruby language. Since i think acts_as_tree
is
a very usual application,as to such an application, Others who has used
it have not found such a phenomena?

Try to use nested_set. See my ehanced version in signature :wink:

Jean-Christophe M.

Better Nested Set for rails:
http://opensource.symetrie.com/trac/better_nested_set