Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is using MySQL BLOB recommended?

I'm coding an application that will be uploading and deleting many files, i usually just move the files to a folder in the server naming them with the row unique id. But as i understand MySQL also lets me store binary data (files) when would this be a better choice?.

Please use solid arguments, like When does using BLOB will mean performance improvement?.

P.S: I'm using MyISAM if that matters.

Thanks.


UPDATE:

Related questions:
- Storing Images in DB - Yea or Nay?
- To Do or Not to Do: Store Images in a Database (thanks to Sebastian)

UPDATE 2

Storing the files in the database is not a need i'm trying to know when is this a better idea than storing them in folders.

like image 398
amosrivera Avatar asked Mar 12 '11 22:03

amosrivera


People also ask

When should BLOB be used?

A blob is a data type that can store binary data. This is different than most other data types used in databases, such as integers, floating point numbers, characters, and strings, which store letters and numbers. Since blobs can store binary data, they can be used to store images or other multimedia files.

Why BLOB is used in MySQL?

Blob is the data type in MySQL that helps us store the object in the binary format. It is most typically used to store the files, images, etc media files for security reasons or some other purpose in MySQL.

Should I use BLOB?

The reason to use BLOBs is quite simply manageability - you have exactly one method to back and restore up the database, you can easily do incremental backups, there is zero risk of the image and its meta data stored in DB tables ever getting out of sync, you also have one programming interface to run queries or load/ ...

What is the maximum size of BLOB in MySQL?

BLOB: Can handle up to 65,535 bytes of data. MEDIUMBLOB: The maximum length supported is 16,777,215 bytes. LONGBLOB: Stores up to 4,294,967,295 bytes of data.


4 Answers

Read:

  • MySQL Binary Storage using BLOB VS OS File System: large files, large quantities, large problems
  • To Do or Not to Do: Store Images in a Database

which concludes

If you on occasion need to retrieve an image and it has to be available on several different web servers. But I think that's pretty much it.

  • If it doesn't have to be available on several servers, it's always better to put them in the file system.
  • If it has to be available on several servers and there's actually some kind of load in the system, you'll need some kind of distributed storage.
like image 179
Sebastian Zaklada Avatar answered Oct 21 '22 23:10

Sebastian Zaklada


If you are using MyISAM db engine then BLOB fields can be indexed so you can perform quick searches on your files using the database.

Also another advantage of storing files in BLOB fields is that they can be accessed more efficiently than files on the disk (there is no need for directory traversal, open, read, close).

If you are planning to store lots of files in MYSQL, it's usually a good practice to have the files stored in a separate table. This allows you to scan the meta info without stumbling over the blobs. Then, when you actually need to fetch a blob, the JOIN is adequately efficient.

like image 30
Roman Avatar answered Oct 22 '22 00:10

Roman


Well, it's a bit old, but this article makes a few decent arguments for BLOB storage: http://www.dreamwerx.net/site/article01.

While not a performance gain per se, having your images and whatnot in a DB as opposed to in a directory should also eliminate problems with hotlinking (assuming this is a web app that's publicly available).

like image 33
Tieson T. Avatar answered Oct 22 '22 01:10

Tieson T.


Memcache is not an alternative solution as you need to manage redundancy and TTL across distributed servers which make it harder to maintain.

The better solution in my opinion is to put public static data on CDN which is distributed by design and private static data on the DB for ease of distribution across multiple servers.

Each server can implement it's own Memcache upon each hit.

If you already stored data in the filesystem and you want to migrate it into database, the easiest way is to create a key,value table of the following:

KEY='/image/filename' (string of the filesystem location), value=BLOB (the actual file) and build a wrapper which will get this from the database with the help of rewrite rule and application handling. This way you can use full transparency with your existing code.

like image 40
guykaplan Avatar answered Oct 22 '22 01:10

guykaplan