# Speed up counting?

**URL:** <https://www.ruby-forum.com/t/speed-up-counting/153606>\
**Category:** Rails\
**Created:** [November 19, 2008, 10:46am UTC](https://www.ruby-forum.com/t/speed-up-counting/153606 "2008-11-19T10:46:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lackyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d07c76/32.png) [@lackyg](https://www.ruby-forum.com/u/lackyg)\
**Post date:** [November 19, 2008, 10:46am UTC](https://www.ruby-forum.com/t/speed-up-counting/153606/1 "2008-11-19T10:46:58Z")

</div>

Hello!

I need to speed up a count of apearances of a distinct category of  
entities in a table. I get the categories with the following command:

@asset\_classes = ActiveRecord::Base.connection.select\_values “SELECT  
DISTINCT CLASS FROM SymbolsMatch\_MASTER ORDER BY CLASS”

Now I need to create a list of this categories together with the number  
of apearance in the table. Momentarily I use this syntax but it is VERY  
slow.

 \<% for asset\_class in @asset\_classes do %\> \<% count = DeviceToUpdate.find(:all, :conditions =\> ["status like 'moreinfo' and CLIENT\_STATUS is NULL and CLASS like ?",asset\_class] ).count if count \> 0 %\> - \<%= truncate(asset\_class, 20, ".") %\> (\<%= count.to\_s %\>)
 \<% end %\> \<% end %\>

Thank you for any suggestion how to speed this up!

lacky

---

<div class="post-metadata">

**Author:** ![lackyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d07c76/32.png) [@lackyg](https://www.ruby-forum.com/u/lackyg)\
**Post date:** [November 19, 2008, 11:00am UTC](https://www.ruby-forum.com/t/speed-up-counting/153606/2 "2008-11-19T11:00:51Z")

</div>

On 19 Nov 2008, at 09:46, Günther Lackner wrote:

> number  
> of apearance in the table. Momentarily I use this syntax but it is  
> VERY  
> slow.

If you just want to count items then use DeviceToUpdate.count …  
which will use an sql count rather than fetching the entire collection  
into memory and counting that (although arrays don’t have a count  
method so i’m not sure how the code below is working at all). An  
appropriate index would also help counting, and/or you could cache the  
result

Fred

---

<div class="post-metadata">

**Author:** ![lackyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d07c76/32.png) [@lackyg](https://www.ruby-forum.com/u/lackyg)\
**Post date:** [November 19, 2008, 11:46am UTC](https://www.ruby-forum.com/t/speed-up-counting/153606/3 "2008-11-19T11:46:04Z")

</div>

Hi!

> If you just want to count items then use DeviceToUpdate.count …

You mean like this?

count = DeviceToUpdate.count(:conditions =\> [“status like ‘moreinfo’ and  
CLIENT\_STATUS is NULL and CLASS like ?”,asset\_class ] )

lacky

---

<div class="post-metadata">

**Author:** ![lackyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d07c76/32.png) [@lackyg](https://www.ruby-forum.com/u/lackyg)\
**Post date:** [November 19, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/speed-up-counting/153606/4 "2008-11-19T12:33:03Z")

</div>

On 19 Nov 2008, at 11:46, GÃ¼nther Lackner wrote:

> > If you just want to count items then use DeviceToUpdate.count …
> 
> You mean like this?
> 
> count = DeviceToUpdate.count(:conditions =\> [“status like ‘moreinfo’  
> and  
> CLIENT\_STATUS is NULL and CLASS like ?”,asset\_class ] )

Also keep in mind that in MySQL InnoDB “like” searches are unindexed  
and notoriously slow in the first place.

Compare it to having the find the number of occurrences of the word  
“rails” in a book. One book offers no index at the back of the book,  
so you have to manually go over every page finding the word (MySQL  
LIKE search), while the other does have one and you only need to count  
the pages it appears on listed next to it (Fulltext indexer)

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![lackyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d07c76/32.png) [@lackyg](https://www.ruby-forum.com/u/lackyg)\
**Post date:** [November 19, 2008, 3:29pm UTC](https://www.ruby-forum.com/t/speed-up-counting/153606/5 "2008-11-19T15:29:48Z")

</div>

Man, you should really take a look at:

[http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html](http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html)

Cheers, Sazima

On Nov 19, 7:46 am, Günther Lackner [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![lackyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d07c76/32.png) [@lackyg](https://www.ruby-forum.com/u/lackyg)\
**Post date:** [November 19, 2008, 3:53pm UTC](https://www.ruby-forum.com/t/speed-up-counting/153606/6 "2008-11-19T15:53:16Z")

</div>

What do you mean?

Sazima wrote:

> Man, you should really take a look at:
> 
> [http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html](http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html)
> 
> Cheers, Sazima
> 
> On Nov 19, 7:46ï¿½am, Gï¿½nther Lackner [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
