When is using MySQL BLOB recommended?

后端 未结 5 1575
萌比男神i
萌比男神i 2020-11-30 07:58

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

相关标签:
5条回答
  • 2020-11-30 08: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).

    0 讨论(0)
  • 2020-11-30 08:28

    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.

    0 讨论(0)
  • 2020-11-30 08:29

    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.

    0 讨论(0)
  • 2020-11-30 08:32

    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.
    0 讨论(0)
  • 2020-11-30 08:34

    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.

    0 讨论(0)
提交回复
热议问题