# Can you help me solve a potential performance problem with Ferret and view counts

**URL:** <https://www.ruby-forum.com/t/can-you-help-me-solve-a-potential-performance-problem-with-ferret-and-view-counts/145905>\
**Category:** Rails\
**Created:** [August 14, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/can-you-help-me-solve-a-potential-performance-problem-with-ferret-and-view-counts/145905 "2008-08-14T22:27:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jamie](https://www.ruby-forum.com/u/jamie)\
**Post date:** [August 14, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/can-you-help-me-solve-a-potential-performance-problem-with-ferret-and-view-counts/145905/1 "2008-08-14T22:27:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jamie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jamie](https://www.ruby-forum.com/u/jamie)\
**Post date:** [August 14, 2008, 11:15pm UTC](https://www.ruby-forum.com/t/can-you-help-me-solve-a-potential-performance-problem-with-ferret-and-view-counts/145905/2 "2008-08-14T23:15:32Z")

</div>

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

> **[De domeinnaam Nedforce.nl is te koop.](http://www.nedforce.nl)**
>
> Deze mooie domeinnaam kan al over een paar minuten van jou zijn. Betalen gaat eenvoudig en vertrouwd via iDEAL. Direct na de betaling ontvang je de verhuiscode per e-mail en kun je aan de slag.

---

<div class="post-metadata">

**Author:** ![jamie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jamie](https://www.ruby-forum.com/u/jamie)\
**Post date:** [August 14, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/can-you-help-me-solve-a-potential-performance-problem-with-ferret-and-view-counts/145905/3 "2008-08-14T23:19:00Z")

</div>

Make that:

self.class.disable\_ferret do  
increment!(:counter)  
end

Note the added bang (!)

–  
Roderick van Domburg

> **[De domeinnaam Nedforce.nl is te koop.](http://www.nedforce.nl)**
>
> Deze mooie domeinnaam kan al over een paar minuten van jou zijn. Betalen gaat eenvoudig en vertrouwd via iDEAL. Direct na de betaling ontvang je de verhuiscode per e-mail en kun je aan de slag.

---

<div class="post-metadata">

**Author:** ![jamie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jamie](https://www.ruby-forum.com/u/jamie)\
**Post date:** [August 15, 2008, 10:04am UTC](https://www.ruby-forum.com/t/can-you-help-me-solve-a-potential-performance-problem-with-ferret-and-view-counts/145905/4 "2008-08-15T10:04:50Z")

</div>

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

> **[De domeinnaam Nedforce.nl is te koop.](http://www.nedforce.nl)**
>
> Deze mooie domeinnaam kan al over een paar minuten van jou zijn. Betalen gaat eenvoudig en vertrouwd via iDEAL. Direct na de betaling ontvang je de verhuiscode per e-mail en kun je aan de slag.

---

<div class="post-metadata">

**Author:** ![jamie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jamie](https://www.ruby-forum.com/u/jamie)\
**Post date:** [August 19, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/can-you-help-me-solve-a-potential-performance-problem-with-ferret-and-view-counts/145905/5 "2008-08-19T20:29:51Z")

</div>

Hi Jamie,

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

Cheers, Sazima

---

<div class="post-metadata">

**Author:** ![jamie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jamie](https://www.ruby-forum.com/u/jamie)\
**Post date:** [August 14, 2008, 11:28pm UTC](https://www.ruby-forum.com/t/can-you-help-me-solve-a-potential-performance-problem-with-ferret-and-view-counts/145905/6 "2008-08-14T23:28:22Z")

</div>

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-

---

<div class="post-metadata">

**Author:** ![jamie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jamie](https://www.ruby-forum.com/u/jamie)\
**Post date:** [August 19, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/can-you-help-me-solve-a-potential-performance-problem-with-ferret-and-view-counts/145905/7 "2008-08-19T21:52:50Z")

</div>

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.
