# Acts\_as\_tree leads to high CPU occupied?

**URL:** <https://www.ruby-forum.com/t/acts-as-tree-leads-to-high-cpu-occupied/65885>\
**Category:** Rails\
**Created:** [August 22, 2006, 3:06am UTC](https://www.ruby-forum.com/t/acts-as-tree-leads-to-high-cpu-occupied/65885 "2006-08-22T03:06:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [August 22, 2006, 3:06am UTC](https://www.ruby-forum.com/t/acts-as-tree-leads-to-high-cpu-occupied/65885/1 "2006-08-22T03:06:52Z")

</div>

ActiveRecord:

class Classify \< ActiveRecord::Base  
acts\_as\_tree :order=\>“name”  
end

template:

 \<% root.children.each do|subtype| %\> - \<%= "#{subtype.name}" %\>
 \<% subtype.children.each do|thirdtype| %\> - \<%= "#{thirdtype.name}" %\>
 \<% end %\> 
 \<% end %\> 

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

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [August 22, 2006, 3:22am UTC](https://www.ruby-forum.com/t/acts-as-tree-leads-to-high-cpu-occupied/65885/2 "2006-08-22T03:22:23Z")

</div>

Andy wrote:

> ActiveRecord:
> 
> class Classify \< ActiveRecord::Base  
> acts\_as\_tree :order=\>“name”  
> end
> 
> template:
> 
> \<% root.children.each do|subtype| %\> - \<%= "#{subtype.name}" %\>
> \<% subtype.children.each do|thirdtype| %\> - \<%= "#{thirdtype.name}" %\>
> \<% end %\> 
> \<% end %\> 
> 
> 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](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [August 22, 2006, 3:48am UTC](https://www.ruby-forum.com/t/acts-as-tree-leads-to-high-cpu-occupied/65885/3 "2006-08-22T03:48:55Z")

</div>

I believe acts\_as\_nested\_set can get the entire tree with one SQL  
statement.

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [August 22, 2006, 8:02am UTC](https://www.ruby-forum.com/t/acts-as-tree-leads-to-high-cpu-occupied/65885/4 "2006-08-22T08:02:37Z")

</div>

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](http://www.railtie.net/articles/2006/03/31/implement-acts_as_threaded-without-a-plugin)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [August 22, 2006, 7:17am UTC](https://www.ruby-forum.com/t/acts-as-tree-leads-to-high-cpu-occupied/65885/5 "2006-08-22T07:17:22Z")

</div>

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](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…

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [August 22, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/acts-as-tree-leads-to-high-cpu-occupied/65885/6 "2006-08-22T16:28:30Z")

</div>

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 😉

## Jean-Christophe M.

> **[Édition et distribution de livres sur la musique, de partitions et de revues...](https://symetrie.com/fr)**
>
> Présentation des publications de Symétrie en musique, musicologie et médecine de la voix.

Better Nested Set for rails:  
[http://opensource.symetrie.com/trac/better\_nested\_set](http://opensource.symetrie.com/trac/better_nested_set)
