Store pictures as files or in the database for a w

2018-12-31 08:09发布

My question is fairly generic and I know there might not be an 100% answer to it. I'm building an ASP .NET web solution that will include a lot of pictures and hopefully a fair amount of traffic. I do really want to achieve performance.

Should I save the pictures in the Database or on the File system? And regardless the answer I'm more interested in why choosing a specific way.

Many thanks, Stefan

DUPLICATE: Storing Images in DB - Yea or Nay?, How to store images in your filesystem, Storing a small number of images: blob or fs? and probably some others.


COMMENT: Thanks for many good answers. I will go for a file based solution even if I like the idea of having a 100% database driven solution. It seems that there are today good solutions to do what I want with databases etc but I have a few reasons for not doing it.

  • I will be on a hosted solution, I have huge amount of storage(10gb) but only 300mb for the database. It will cost a lot for extra storage in the DB.

  • I'm not an DB expert and as well not in control of settings of the DB. A DB based solution might need custom configuration as it looks like.

If we will move to run the site on our own server I might consider a DB based solution. thanks, Stefan

10条回答
临风纵饮
2楼-- · 2018-12-31 08:47

Storing images in the database adds a DB overhead to serve single images and makes it hard to offload to alternate storage (S3, Akami) if you grow to that level. Storing them in the database makes it much easier to move your app to a different server since it's only the DB that needs to move now.

Storing images on the disk makes it easy to offload to alternate storage, makes images static elements so you don't have to mess about with HTTP headers in your web app to make the images cacheable. The downside is if you ever move your app to a different server you need to remember to move the images too; something that's easily forgotten.

查看更多
琉璃瓶的回忆
3楼-- · 2018-12-31 08:47

For web based applications, you're going to get better performance out of using the file system for storing your images. Doing so will allow you to easily implement caching of the images at multiple levels within your application. There are some advantages to storing images in a database, but most of the time those advantages come with client based applications.

查看更多
笑指拈花
4楼-- · 2018-12-31 08:47

Why not choose a individual NoSql database to store your files.

It brings you with data integrity ,data consistency as @chburd mentioned.

While you rdbms still keep small.

查看更多
情到深处是孤独
5楼-- · 2018-12-31 08:50

Better to store files as files. Different databses handle Blob data differently, so if you have to migrate your back end you might get into trouble.

When serving the impages an < img src= to a file that already exists on the server is likely to be quicker than making a temporary file from the database field and pointing the < img tag to that.

I found this answer from googling your question and reading the comments at http://databases.aspfaq.com/database/should-i-store-images-in-the-database-or-the-filesystem.html

查看更多
谁念西风独自凉
6楼-- · 2018-12-31 08:52

i usually like to have binary files in the database because :

  • data integrity : no unreferenced file, no path in the db without any file associated
  • data consistency : take a database dump and that's all. no "O i forgot to targz this data directory."
查看更多
冷夜・残月
7楼-- · 2018-12-31 08:54

The adage has always been "Files in the filesystem, file metadata in the database"

查看更多
登录 后发表回答