Ruby Forum Ruby on Rails > Can you help me solve a potential performance problem with Ferret and view counts

Posted by jamie (Guest)
on 14.08.2008 22:27
(Received via mailing list)
I am currently using Ferret and the DRb server for search and can
forsee a potential problem which I am hoping some of you good folks
can help me with.

I currently have a view count field associated with a record, which is
incremented every time the view for the record is displayed.

This field is part of the search index such that we can search and
sort by the "most viewed" records.

The potential performance issue is that each time the page is viewed,
the counter is incremented, and this triggers the index to be rebuilt
for the associated record.

I have considered disabling the after_save callbacks method but feel
this is taking a hammer to the problem, as in most cases I do need the
index to be updated immediately after the record is updated.

I am assuming implementing a view count and using for search is not an
uncommon requirement... so hoping there is a good solution.
Posted by Roderick van Domburg (roderickvd)
on 14.08.2008 23:15
Hi Jamie,

> The potential performance issue is that each time the page is viewed,
> the counter is incremented, and this triggers the index to be rebuilt
> for the associated record.

You can work around this by placing the counter update in a 
disable_ferret block:

  self.class.disable_ferret do
    increment(:counter)
  end

--
Roderick van Domburg
http://www.nedforce.nl
Posted by Roderick van Domburg (roderickvd)
on 14.08.2008 23:19
Make that:

  self.class.disable_ferret do
    increment!(:counter)
  end

Note the added bang (!)

--
Roderick van Domburg
http://www.nedforce.nl
Posted by jamie (Guest)
on 14.08.2008 23:28
(Received via mailing list)
Thanks Roderick...one question... since I still do want to include the
view count in the index... does that imply a periodic CRON job for
updating this field within the Ferret index?  Is there a way to
rebuild only a subset of the fields within the index?

On Aug 14, 5:19 pm, Roderick van Domburg <rails-mailing-l...@andreas-
Posted by Roderick van Domburg (roderickvd)
on 15.08.2008 10:04
Hi Jamie,

> Thanks Roderick...one question... since I still do want to include the
> view count in the index... does that imply a periodic CRON job for
> updating this field within the Ferret index?  Is there a way to
> rebuild only a subset of the fields within the index?

I don't believe that it's possible to update a subset of columns: the 
other indexed columns would then be set to nil. Performance would not 
improve that much because the indexed row needs to be rewritten anyway.

So yes, a periodic update on the entire object would suffice if you 
don't require real-time counts in the index.

--
Roderick van Domburg
http://www.nedforce.nl
Posted by Sazima (Guest)
on 19.08.2008 20:29
(Received via mailing list)
Hi Jamie,

I really wouldn't use ferret for this. Use the DB to show the most
viewed...

Cheers, Sazima
Posted by Brian Hogan (Guest)
on 19.08.2008 21:52
(Received via mailing list)
Can you put the counts in their own table, with foreign keys to the 
original
record?

Then you need only issue the update to that model....

c = Counter.find_by_thing_id(25)
c.count +=1
c.save

just thinking out loud.