所以我用的是一个在数据库中大量存储图像的应用程序。你对此有什么看法?我更倾向于将位置存储在文件系统中,而不是直接存储在DB中。

你认为优点和缺点是什么?


当前回答

I'm not sure how much of a "real world" example this is, but I currently have an application out there that stores details for a trading card game, including the images for the cards. Granted the record count for the database is only 2851 records to date, but given the fact that certain cards have are released multiple times and have alternate artwork, it was actually more efficient sizewise to scan the "primary square" of the artwork and then dynamically generate the border and miscellaneous effects for the card when requested.

这个图像库的最初创建者创建了一个数据访问类,它根据请求呈现图像,并且对于查看和单独的卡片来说,它的速度非常快。

This also eases deployment/updates when new cards are released, instead of zipping up an entire folder of images and sending those down the pipe and ensuring the proper folder structure is created, I simply update the database and have the user download it again. This currently sizes up to 56MB, which isn't great, but I'm working on an incremental update feature for future releases. In addition, there is a "no images" version of the application that allows those over dial-up to get the application without the download delay.

到目前为止,这个解决方案工作得很好,因为应用程序本身被定位为桌面上的单个实例。有一个网站将所有这些数据存档,以供在线访问,但我绝不会使用相同的解决方案。我同意文件访问更可取,因为它可以更好地适应图像请求的频率和数量。

希望这不是太多的废话,但我看到了这个主题,并想从一个相对成功的中小型应用程序中提供一些我的见解。

其他回答

这可能有点不切实际,但如果你正在使用(或计划使用)SQL Server 2008,我建议你看看新的FileStream数据类型。

FileStream解决了在DB中存储文件的大部分问题:

blob实际上以文件的形式存储在文件夹中。 可以使用数据库连接或通过文件系统访问blob。 备份是集成的。 迁移“就是管用”。

然而,SQL的“透明数据加密”不加密FileStream对象,因此如果考虑到这一点,最好将它们存储为varbinary。

摘自MSDN文章:

Transact-SQL语句可以插入、更新、查询、搜索和备份FILESTREAM数据。Win32文件系统接口提供对数据的流访问。 FILESTREAM使用NT系统缓存来缓存文件数据。这有助于减少FILESTREAM数据对数据库引擎性能的影响。SQL Server缓冲池未被使用;因此,该内存可用于查询处理。

The word on the street is that unless you are a database vendor trying to prove that your database can do it (like, let's say Microsoft boasting about Terraserver storing a bajillion images in SQL Server) it's not a very good idea. When the alternative - storing images on file servers and paths in the database is so much easier, why bother? Blob fields are kind of like the off-road capabilities of SUVs - most people don't use them, those who do usually get in trouble, and then there are those who do, but only for the fun of it.

我曾经开发过一个图像处理应用程序。我们将上传的图像存储在类似于/images/[今天的日期]/[id号]的目录中。但是我们还从图像中提取了元数据(exif数据),并将其连同时间戳等一起存储在数据库中。

我会选择两种解决方案,我的意思是……我将开发一个小组件(EJB),它将映像存储在DB中,并将映像存储到服务器的路径。只有当我们有一个新的图像或原始图像更新时,这个DB才会更新。然后,我还将该路径存储在业务DB中。

从应用程序的角度来看,我将始终使用文件系统(从业务DB检索路径),通过这种方式,我们将修复备份问题,并避免可能的性能问题。

唯一的缺点是我们将存储相同的图像2次…好的一点是内存很便宜,拜托!

尝试使用SQL模拟文件系统通常是一个糟糕的计划。如果您坚持使用文件系统作为外部存储,那么最终编写的代码会更少,结果也会更好。