Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL TINYBLOB vs LONGBLOB

This is a follow up for my previous question: Blob and Storage Requirement

I did some testing using SHOW TABLE STATUS, and found out that the total disk space used actually only depends on the size of actual files uploaded to the database and not on the type (e.g. TINYBLOB or LONGBLOG) of the column.

So, if that's not the case then what difference does make when we choose one over the other of the BLOB types?

like image 255
jombie Avatar asked Jan 20 '12 18:01

jombie


2 Answers

Each size of blob field reserves extra bytes to hold size information. A longblob uses 4+n bytes of storage, where n is the actual size of the blob you're storing. If you're only ever storing (say) 10 bytes of blob data, you'd be using up 14 bytes of space.

By comparison, a tinyblob uses 1+n bytes, so your 10 bytes would occupy 11 bytes of space, a 3 byte savings.

3 bytes isn't much when dealing with only a few records, but as DB record counts grow, every byte saved is a good thing.

like image 198
Marc B Avatar answered Sep 29 '22 23:09

Marc B


Using BLOB make your size being proportional with the size of the files not with the number of the files as in normal database fields (BLOB is not allocated in the records space - except for size and an internal link to file data) The big difference between BLOB types comes from the allowed size of the images (see link). As BLOB(long) adds only 3 bytes more when working with large images, I noticed that most programs use BLOB(long) - it is an immaterial cost when you compare 3 bytes with 1M+ bytes for images, and programmers choose BLOB(long) to avoid restructuring the database as their creation grows.

https://tableplus.com/blog/2019/10/tinyblob-blob-mediumblob-longblob.html

like image 42
Dorin Avatar answered Sep 29 '22 22:09

Dorin