When is using MySQL BLOB recommended?

2019-01-07 19:18发布

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.

5条回答
beautiful°
2楼-- · 2019-01-07 19:58

Are you bound to using MySQL? If not, try an ODBMS or PostgreSQL to store files, or you could store just the paths for the files. See this for instance.

查看更多
神经病院院长
3楼-- · 2019-01-07 20:02

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.

查看更多
该账号已被封号
4楼-- · 2019-01-07 20:09

Read:

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.
查看更多
Emotional °昔
5楼-- · 2019-01-07 20:13

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.

查看更多
ら.Afraid
6楼-- · 2019-01-07 20:21

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).

查看更多
登录 后发表回答