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