# Isn't there any performance issue when saving serialized attributes every time?

**URL:** <https://www.ruby-forum.com/t/isnt-there-any-performance-issue-when-saving-serialized-attributes-every-time/173808>\
**Category:** Rails\
**Created:** [August 15, 2009, 12:09pm UTC](https://www.ruby-forum.com/t/isnt-there-any-performance-issue-when-saving-serialized-attributes-every-time/173808 "2009-08-15T12:09:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![clive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f08c70/32.png) [@clive](https://www.ruby-forum.com/u/clive)\
**Post date:** [August 15, 2009, 12:09pm UTC](https://www.ruby-forum.com/t/isnt-there-any-performance-issue-when-saving-serialized-attributes-every-time/173808/1 "2009-08-15T12:09:04Z")

</div>

In edge rails, serialized attributes are saved every time no matter  
they are changed or not:  
def update\_with\_dirty  
if partial\_updates?  
# Serialized attributes should always be written in case  
they’ve been  
# changed in place.  
update\_without\_dirty(changed | (attributes.keys &  
self.class.serialized\_attributes.keys))  
else  
update\_without\_dirty  
end  
end

In out app, User model has a serialized attribute friend\_ids, which is  
the id array of the user’s friends (used to be a friendships table,  
but when that table grew to tens of millions of records, we refactored  
it to a User’s attribute). The User model in our app is saved very  
frenquently, and many users have more than 1 handred friends, so the  
friend\_ids may has 1kB long. Some of my colleages opposed to use  
serialized friend\_ids, and suggested to used a comma separated string  
instead because saving 1kB each time would have performance issue.

---

<div class="post-metadata">

**Author:** ![clive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f08c70/32.png) [@clive](https://www.ruby-forum.com/u/clive)\
**Post date:** [August 15, 2009, 12:17pm UTC](https://www.ruby-forum.com/t/isnt-there-any-performance-issue-when-saving-serialized-attributes-every-time/173808/2 "2009-08-15T12:17:16Z")

</div>

On Aug 15, 11:08 am, Clive [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> update_without_dirty
> 
> ```
> 
> instead because saving 1kB each time would have performance issue.  
> Well before rails 2.1 partial updates didn’t exist at all and the  
> world continued to function 🙂  
> The simple to solution is to benchmark it - monkey patch stuff so that  
> this does happen, measure, monkey patch it so that is doesn’t, measure  
> again.

Another possible way would be to stick the friend\_ids data in its own  
table. This might also save you loading it unecessarily

Fred

---

<div class="post-metadata">

**Author:** ![clive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f08c70/32.png) [@clive](https://www.ruby-forum.com/u/clive)\
**Post date:** [August 15, 2009, 2:40pm UTC](https://www.ruby-forum.com/t/isnt-there-any-performance-issue-when-saving-serialized-attributes-every-time/173808/3 "2009-08-15T14:40:42Z")

</div>

The world continued to function because few people used rails in high  
profile website at that time. Our app has more than 50 million HTTP  
requests per day, so we have to hack a lot of rails plugins that have  
performance issues under such load:(  
The patch proposed here would fix this issue:  
[https://rails.lighthouseapp.com/projects/8994/tickets/2764-supporting-partial-updates-for-serialized-columns](https://rails.lighthouseapp.com/projects/8994/tickets/2764-supporting-partial-updates-for-serialized-columns)  
I hope it will be merged to edge rails

On Aug 15, 6:15 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![clive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f08c70/32.png) [@clive](https://www.ruby-forum.com/u/clive)\
**Post date:** [August 15, 2009, 5:33pm UTC](https://www.ruby-forum.com/t/isnt-there-any-performance-issue-when-saving-serialized-attributes-every-time/173808/4 "2009-08-15T17:33:22Z")

</div>

On Aug 15, 8:39 am, clive [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The world continued to function because few people used rails in high  
> profile website at that time. Our app has more than 50 million HTTP  
> requests per day, so we have to hack a lot of rails plugins that have  
> performance issues under such load:(  
> The patch proposed here would fix this issue:[#2764 Supporting partial updates for serialized columns - Ruby on Rails - rails](https://rails.lighthouseapp.com/projects/8994/tickets/2764-supporting)…  
> I hope it will be merged to edge rails

No, actually, it won’t. Unless you’re claiming that the bottleneck is  
in transmitting the data to the DB, that patch still converts every  
serialized attribute to YAML almost every time that the record is  
saved (note the calls to object\_to\_yaml). Serialized fields that are  
changed will get converted _again_. So if anything, the overhead may  
be worse than before…

I also find the idea of taking one of the primary parts of a social  
networking site (friend linking) and making it opaque through  
serialization to be very odd. Isn’t the whole point of the “social  
media” fad to be analyzing and drawing conclusions from the network  
graph? Meh.

–Matt J.

---

<div class="post-metadata">

**Author:** ![clive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f08c70/32.png) [@clive](https://www.ruby-forum.com/u/clive)\
**Post date:** [August 16, 2009, 4:46am UTC](https://www.ruby-forum.com/t/isnt-there-any-performance-issue-when-saving-serialized-attributes-every-time/173808/5 "2009-08-16T04:46:28Z")

</div>

Transforming to/from yml consumes CPU cycles, but storing a large  
field to database consumes network bandwidth and disk IO:(  
In this case, I am afraid my colleague’s solution is right: save that  
list as a comma separate string.

Your 2nd point is right, I will consider not storing friend list  
later. But besides friend list, there are other fields that need to  
stored as serialized attr.

Thanks
