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. <ul> <% 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 %> <li> <a href="<%= url_for(:controller => :more_info, :action => :show_class, :class_name => asset_class) %>"><%= truncate(asset_class, 20, ".") %> (<%= count.to_s %>)</a> </li> <% end %> <% end %> </ul> Thank you for any suggestion how to speed this up! lacky -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 19 Nov 2008, at 09:46, Günther Lackner wrote:> > 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. >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> > > > > <ul> > <% 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 %> > <li> > <a href="<%= url_for(:controller => :more_info, :action => > :show_class, :class_name => asset_class) %>"><%= truncate(asset_class, > 20, ".") %> (<%= count.to_s %>)</a> > </li> > <% end %> > <% end %> > </ul> > > > > > > Thank you for any suggestion how to speed this up! > > lacky > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Man, you should really take a look at: http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html Cheers, Sazima On Nov 19, 7:46 am, Günther Lackner <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. > > <ul> > <% 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 %> > <li> > <a href="<%= url_for(:controller => :more_info, :action => > :show_class, :class_name => asset_class) %>"><%= truncate(asset_class, > 20, ".") %> (<%= count.to_s %>)</a> > </li> > <% end %> > <% end %> > </ul> > > Thank you for any suggestion how to speed this up! > > lacky > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
What do you mean? Sazima wrote:> Man, you should really take a look at: > > http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html > > Cheers, Sazima > > On Nov 19, 7:46�am, G�nther Lackner <rails-mailing-l...@andreas-s.net>-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---