save user images in MYSQL database by which method? [closed]

Efficiency can mean many things. Maintainability and scalability are as important as pure performance when you design a solution.

Method 2 depends on files on disk which means that in a production environment with multiple servers (which every prod env should have) you will have to keep the files in sync between all servers in your solution.

You get comments on your question that say that Method 2 is more popular. That may be, but it is harder to scale and to keep in sync.

Method 1 (keeping the pictures in the database) is the way to go as that makes them location-independent! But I would make one change: put all the pictures in a separate table! Why? All databases read/write/search data in IO pages towards the disk. By keeping the User table small you get better query performance on that table. If you add the picture to the User table it will grow in size and fewer pages will fit on the same page when reading from disk.

It is true that when you need to read a picture it will mean another query, but as pictures almost never change you can cache them either locally of in a distributed cache to allow fast access.

What about performance?
Method 2 can be seen as faster as you don’t need to read the pictures from the disk on database, but it still needs to be read from the disk on of the local server… So there is really no advantage. But even if there was an advantage that advantage would disappear as soon as your most frequent pictures have been added to the cache, then using the Method 1 is probably even quicker than Method 2.

Leave a Comment