Search code examples
database-performance

Saving image in database


Is it good to save an image in database with type BLOB?
or save only the path and copy the image in specific directory?

Which way is the best (I mean good performance for the database and the application) and why?


Solution

  • What are your requirements?

    In the vast majority of cases saving the path will be better, simply because of the sheer size of the files compared to the rest of data (bulge the DB by GBs due to image inclusion). Consider adding an indirection, eg. save the path as a name and a reference to a storage resource (eg. a storage_id referencing a row in storages tables) and the path attached to the 'storage'. This way you can easily move files (copy all files, then update the storage path, rather than update 1MM individual paths).

    However, if your requirements include consistent backup/restore and/or disaster recoverability, is often better to store images in the DB. Is not easier, nor more convenient, but is simply going to be required. Each DB has its own way of dealing with this problem, eg. in SQL Server you would use a FILESTREAM type which allows remote access via file access API. See FILESTREAM MVC: Download and Upload images from SQL Server for an example.

    Also, a somehow dated but none the less interesting paper on the topic: To BLOB or Not to BLOB.