使用文件流来存储图像和缩略图是个好主意吗?
我们正在设计一个 .NET 应用程序,用于存储和搜索大量图像 (>100.000)。
当我们上传图像时,我们需要创建缩略图并将原始图像和缩略图存储在数据库(SQL Server 2008 R2)中。
我读到存储图像的最佳方法是使用 FILESTREAM
http://msdn .microsoft.com/en-us/library/cc949109.aspx,
但是当图像较小时,这是低效的。
使用文件流存储图像和缩略图是个好主意吗?有更好的选择吗?
We are designing a .NET application, that stores and search a large amount of images (>100.000).
When we upload an image, we need to create a thumbnail and store the original image and the thumbnail in the DB (SQL Server 2008 R2).
I've read that the best way to store images is using FILESTREAM
http://msdn.microsoft.com/en-us/library/cc949109.aspx
but when the images are smaller this is ineficient.
Is a good idea to store images and thumbnails using filestream? There is better alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
微软研究院有一篇非常好的论文,名为To Blob or Not To Blob 深入讨论了这个主题。
他们经过大量性能测试和分析后得出的结论是:
如果您的图片或文档大小通常低于 256K,将它们存储在数据库
VARBINARY
列中效率更高如果您的图片或文档大小通常超过 1 MB,则将它们存储在文件系统中更有效率(使用 SQL Server 2008 的
FILESTREAM
属性,它们仍然处于事务控制之下并且是数据库的一部分)在这两者之间,它有点 在这两者之间,根据您的用途进行折腾,
如果您决定将图片放入 SQL Server 表中,我强烈建议您使用单独的表存储这些图片 - 不要将员工照片存储在员工表中 - 将它们保存在单独的表中。这样,假设您并不总是需要选择员工照片作为查询的一部分,那么 Employee 表就可以保持精简、简洁且非常高效。
对于文件组,请查看文件和文件组体系结构了解简介。基本上,您可以从一开始就为大型数据结构创建具有单独文件组的数据库,或者稍后添加其他文件组。我们称之为“LARGE_DATA”。
现在,每当您要创建一个需要存储
VARCHAR(MAX)
或VARBINARY(MAX)
列的新表时,您都可以为大数据指定此文件组:查看有关文件组的 MSDN 介绍,并尝试一下!
There's a really good paper by Microsoft Research called To Blob or Not To Blob that discusses this topic in depth.
Their conclusion after a large number of performance tests and analysis is this:
if your pictures or document are typically below 256K in size, storing them in a database
VARBINARY
column is more efficientif your pictures or document are typically over 1 MB in size, storing them in the filesystem is more efficient (and with SQL Server 2008's
FILESTREAM
attribute, they're still under transactional control and part of the database)in between those two, it's a bit of a toss-up depending on your use
If you decide to put your pictures into a SQL Server table, I would strongly recommend using a separate table for storing those pictures - do not store the employee foto in the employee table - keep them in a separate table. That way, the Employee table can stay lean and mean and very efficient, assuming you don't always need to select the employee foto, too, as part of your queries.
For filegroups, check out Files and Filegroup Architecture for an intro. Basically, you would either create your database with a separate filegroup for large data structures right from the beginning, or add an additional filegroup later. Let's call it "LARGE_DATA".
Now, whenever you have a new table to create which needs to store
VARCHAR(MAX)
orVARBINARY(MAX)
columns, you can specify this file group for the large data:Check out the MSDN intro on filegroups, and play around with it!
实际上正确的答案 - 这取决于。
如果图像相对较小 - 您可以将其保留在 varbinary(max) 字段中。
为什么不使用文件流? - FILESTREAM 性能在大 BLOB 值上达到最佳 - 1Mb+
Actually the proper answer - it depends.
If the images are relatively small - you can keep it in varbinary(max) field.
Why not FILESTREAM? - The FILESTREAM performance reaches its best on large BLOB values - 1Mb+