Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving attachments to database: blob vs path reference

I am in a process of designing a system where one can create an entry and then add attachments to that entry, which is then saved into a database. However I am of two minds on how to implement the attachment handling.

I am battling between two approaches:

  1. Save the attachments to the database directly as blob field type
  2. Save to the database the directory path into the file and save the actual file to the server

Now as far as I can see both of these approaches has pros and cons.

1st approach would keep all the data in the same place so I can easily move it elsewhere if I want to. Also it keeps things consistent as far as "where is the data" guestion is concerned. On the other hand I have heard that saving things like that is very resource hog and the database size will grow extensively.

2nd approach would allow the database to keep to the easily managed data fields like text and numbers while the more heavier stuff is on the server. This would also allow a more direct access to files if necessary. On the other hand the data is seperated and more difficult to keep track of and would also need to introduce file system access in order to retrieve the said files.

I am leaning towards the number 1 approach but to this end I am hoping an answer to the following:

How would saving attachments directly to database impact on the database size and application performance?

like image 704
Tomkarho Avatar asked Dec 21 '13 16:12

Tomkarho


People also ask

Should you store BLOBs in a database?

However, storing BLOB data can dramatically increase the size of your databases. the file system involves a bit less overhead than reading data from a database. And without the BLOBs, your databases tend to be smaller.

Is it better to store images in database or filesystem?

Generally databases are best for data and the file system is best for files. It depends what you're planning to do with the image though. If you're storing images for a web page then it's best to store them as a file on the server. The web server will very quickly find an image file and send it to a visitor.

Is it a good idea to save images in database?

Storing images in a database table is not recommended. There are too many disadvantages to this approach. Storing the image data in the table requires the database server to process and traffic huge amounts of data that could be better spent on processing it is best suited to.


2 Answers

Don't store documents as blobs in the database. Store paths. RDBMS is not a document store. MySQL is not designed for this purpose and storing documents inside MySQL will make the database size unnecessarily huge and data retrieval almost impossible, taking backups will take a very long time and if something happens to your database all your files go with it. File store is almost always a better choice for file storage than a RDBMS.

like image 199
Sam Avatar answered Oct 10 '22 11:10

Sam


I have a similar problem, I think the approach I take is depending on the type of attachment that is needed In my case what I did is open the attachment which is mostly text and simply place the data as in in the DB (instead of the DB simply storing the attachment)

This allows me to look also inside the content in an easy way (of course this relates to the number of updates that you expect)

like image 27
Shimonbd Avatar answered Oct 10 '22 11:10

Shimonbd