Doubt about saving an image in database

4

I have a question about saving an image in a database, in specific mysql, my question is what would occupy more space in the server, save the bytes of the image in a field of type bytes or save the image in a specific directory and then that directory to save it in a field of type varchar , to then know which directory can go to find the image and show it

If I save the bytes of the image like this: let's say I have this table

   int  bytes
   id | imagen
   ---------------
   1  | bytes[ ]
   2  | bytes[ ]

I think that if I save it in this way the server will work more because to save it I would have to obtain the byte array of the image and then perform the insert of the array, then to be able to visualize the image I opened it to reconstruct it.

now if I keep it this way:

 int  varchar
 id | imagen
 ---------------
 1  | mipagina.com/imagenes/test.png
 2  | mipagina.com/imagenes/test1.png

would occupy less space in the database but the image would be saved in the specific directory, the disk space would increase faster because there are images that weigh more than 1MB.

What is more feasible? Someone who shares their experience, would appreciate your opinions. Greetings.

    
asked by J. Armando 22.08.2016 в 23:33
source

2 answers

7

I recommend reading MySQL Binary Storage using BLOB VS OS File System: large files, large quantities, large problems and To Do or Not to Do: Store Images in a Database

In summary and like the majority of the cases in computer science, it depends on your system. If you are going to save 100 images of 1MB and your system will not grow exponentially in time, save them in the BD, which will help you to develop faster. Now, if your system exploits users, access to images and / or quantity of images, it is advisable to save only the paths of these in BD, so you avoid that your BD grows in an uncontrolled way, also, if you need to make any post -processing your images, only programs a script that reads from a directory and you do not have to be hitting the BD one and other times. Among other things to consider, is that storage at this time is almost marginal at the cost level, so you could build a distributed service for your images on several servers, resilient, with low load times, distributed and beautiful. / p>

Now, personally speaking, even if your system is large or small, I do not recommend under any circumstances to save the images in the BD. Why?

  • Making backups is more expensive: you write MANY more bytes
  • When writing many more bytes, and the images are binary and compressed, running a tar.gzip / xz / bzip2 / etc, will make your backup file not compress efficiently and you will have to move many more data to make a backup
  • Come on! the BD is to save DATA!
  • How do you easily access your image? programming an endpoint in some language, which should:

  • Connect to the BD
  • Consult the BLOB data
  • Deduce the mimetype of your image (come on, this could be saved, right?)
  • Write the content of your image
  • Close the connection
  • If you save them in a directory, you can configure your webserver (Nginx for example), so that all of you have a specific subdomain, which will serve the images in the directory they are in, thanks to which you can later, move your images to another server and do load balancing

Remember, in computing, the maxim is always: "Divide and conquer" .

    
answered by 23.08.2016 / 10:37
source
0

Saving the bytes in the database is the least expensive.

In the second way, in addition to the disk space that you occupy, you have the search time and to that you have to add the access time to the disk and the latency of it.

Not to mention if you have to apply a backup policy ... it is easier and faster to backup a database.

I hope it helps you.

Greetings

    
answered by 22.08.2016 в 23:51