To Do or Not to Do: Store Images in a Database [du

2018-12-31 20:04发布

This question already has an answer here:

In the context of a web application, my old boss always said put a reference to an image in the database, not the image itself. I tend to agree that storing an url vs. the image itself in the DB is a good idea, but where I work now, we store a lot of images in the database.

The only reason I can think of is perhaps it's more secure? You don't want someone having a direct link to an url? But if that is the case, you can always have the web site/server handle images, like handlers in asp.net so that a user needs to authenticate to view the image. I'm also thinking performance would be hurt by pulling out the images from the database. Any other reasons why it might be a good/not so good idea to store images in a database?


Exact Duplicate: User Images: Database or filesystem storage?
Exact Duplicate: Storing images in database: Yea or nay?
Exact Duplicate: Should I store my images in the database or folders?
Exact Duplicate: Would you store binary data in database or folders?
Exact Duplicate: Store pictures as files or or the database for a web app?
Exact Duplicate: Storing a small number of images: blob or fs?
Exact Duplicate: store image in filesystem or database?

14条回答
冷夜・残月
2楼-- · 2018-12-31 20:45

This really seems like a KISS (keep it simple stupid) problem. File systems are made to easily handle storing picture files, but it is not easy to do in a database and easy to mess up the data. Why take a performance hit and all the difficulty in the sql and rendering when you can just worry about file security? You can also handle mixed systems ewith NFS or CIFS. File systems are mature technologies. Much simpler, more robust.

查看更多
一个人的天荒地老
3楼-- · 2018-12-31 20:49

If you on occasion need to retrieve an image and it has to be available on several different web servers. But I think that's pretty much it.

  • If it doesn't have to be available on several servers, it's always better to put them in the file system.
  • If it has to be available on several servers and there's actually some kind of load in the system, you'll need some kind of distributed storage.

We're talking an edge case here, where you can avoid adding an additional level of complexity to your system by leveraging the database.

Other than that, don't do it.

查看更多
墨雨无痕
4楼-- · 2018-12-31 20:50

I understand that the majority of database professionals will cross their fingers and hiss at you if you store images in the database (or even mention it). Yes, there are definitely performance and storage implications when using the database as the repository for large blocks of binary data of any kind (images just tend to be the most common bits of data that can't be normalized). However, there are most certainly circumstances where database storage of images is not only allowable but advisable.

For instance, in my old job we had an application where users would attach images to several different points of a report that they were writing, and those images had to be printed out when it was done. These reports were moved about via SQL Server replication, and it would have introduced a HUGE headache to try to manage these images and file paths across multiple systems and servers with any sort of reliability. Storing them in the database gave us all of that "for free," and the reporting tool didn't have to go out to the file system to retrieve the image.

查看更多
千与千寻千般痛.
5楼-- · 2018-12-31 20:52

I stored images in a database for a demonstration application. The reason I did it was security - deleting a record that I shouldn't have wasn't a big problem, but deleting a file I shouldn't have might have been a problem!

If performance became an issue, I would have investigated whether rogue file deletion was a real possibility or not.

查看更多
呛了眼睛熬了心
6楼-- · 2018-12-31 20:56

One of the advantages of storing images into database is that it's portable across the systems and independent on filesystem(s) layout.

查看更多
余欢
7楼-- · 2018-12-31 20:59

disaster recovery is absolutely no fun when you have terabytes of image data stored in the database. You're better off finding a better way to distribute your data to make it more reliable etc... Of course all the overhead (mentioned above) is multiplied when replicating and so on...

Just don't do it!

查看更多
登录 后发表回答