Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing arrays in database : JSON vs. serialized array

With ruby-on-rails, I want to store an array of 3 elements: the last 3 comments of a post. I know I could join the Comment table to the Post one, but I would avoid to do this heavy request for scaling purposes.

So I was wondering what was the best way to store those 3 elements, as I would like to update them easily every time a new comment is made: remove the last comment and add the new one.

What is the correct way to do this ? Store it in a serialized array or in a JSON object ?

like image 743
titibouboul Avatar asked Jan 23 '14 15:01

titibouboul


People also ask

What is the difference between JSON and serialization?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.

Is an array JSON serializable?

NumPy array is not JSON serializable.

Why does JSON need to be serialized?

The purpose of serializing it into JSON is so that the message will be a format that can be understood and from there, deserialize it into an object type that makes sense for the consumer.

What is a serialized array?

The serialize array function is a built-in function in PHP. The serialization of data means converts a value into a sequence of bits to be stored in a memory buffer, in a file, or transfer across a network.

How do I store an array into the database?

To store an array into the database, there are 2 possible alternatives: Convert and store the array as a flat string, using json_encode (), serialize (), or implode ().

Should we serialize production data in a database?

Future requirements (that we don’t know yet) might be hard to achieve if we’ve some production data serialized in a database. It might be a valuable option tough for data storage tables when the client only reads the content and performance is not of great importance (like reporting).

Is it better to use an array or a database?

Databases are good for holding data for long periods of time. Another reason someone may use a database is if you are storing large amounts of data. An advantage with arrays over databases is the speed. A database call is going to be much slower than accessing an element in an array. It sounds to me you would be best using an array.


1 Answers

You can store Arrays and Hashes using ActiveRecord's serialize declaration:

class Comment < ActiveRecord::Base   serialize :stuff end  comment = Comment.new  # stuff: nil comment.stuff = ['some', 'stuff', 'as array'] comment.save comment.stuff # => ['some', 'stuff', 'as array'] 

You can specify the class name that the object type should equal to (in this case Array). This is more explicit and a bit safer. You also won't have to create the array when you assign the first value, since you'll be able to append to the existing (empty) array.

class Comment < ActiveRecord::Base   serialize :stuff, Array end  comment = Comment.new  # stuff: [] comment.stuff << 'some' << 'stuff' << 'as array' 

You can even use a neater version called store: http://api.rubyonrails.org/classes/ActiveRecord/Store.html

This should handle your use case using a built in method.

like image 193
DiegoSalazar Avatar answered Sep 19 '22 22:09

DiegoSalazar