1

With PHP, I am storing serialized objects in a MySQL database.

What column type should I use? I am currently using "LONGTEXT" since basically it's a string.

It's working but I worry that bigger objects might not save correctly.

What column type should I use?

4
  • Have to wonder whether it would actually make more sense to not serialize, and put them in normalised table structure. Commented Jul 4, 2017 at 15:45
  • Why? Because of length limit? Commented Jul 4, 2017 at 15:47
  • That's one factor, another one in ability to search and index columns, you can see the relations more easily, you can end up reducing duplicate data etc etc. The list goes on. Commented Jul 4, 2017 at 15:48
  • Yes, you might be correct by saying that. Some data I might have to store in a table to run queries on. I am just trying to understand in what cases I can store a serialized object. Commented Jul 4, 2017 at 15:50

2 Answers 2

2

Always use BLOB type to store serialized objects.

use MEDIUMBLOB or just BLOB, as long as those types are sufficient to store your serialized data.

BLOB type holds larger object and are binary safe.

Read more about BLOB

Sign up to request clarification or add additional context in comments.

2 Comments

A binary large object column with a maximum length of 16777215 (2^24 - 1) characters. A binary large object column with a maximum length of 4294967295 (2^32 - 1) characters. A BLOB can be 65535 bytes maximum. If you need more consider using a MEDIUMBLOB for 16777215 bytes or a LONGBLOB for 4294967295 bytes.
It depends on your serialization method. JSON is UTF-8 and should be stored as such, and MySQL 5.6 has a JSON column type.
0

BLOB, can hold various types of data.

https://dev.mysql.com/doc/refman/5.7/en/blob.html

This was the column type i used for storing objects in a database recently.

Hope this helps

5 Comments

Thanks! Using LONGBLOB, you know what is the length limit?
My assumption is you dont need LONGBLOB, its size limit is 4GB
Thanks! You know where can I find the size limits for each BLOB?
A good answer to that can be found in this question: stackoverflow.com/questions/5775571/…
Please mark my answer as correct if you're sorted with everything you need now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.