专业&在网站的 SQL Server 中存储文件(图片)的缺点
我正在创建一个 Asp.Net MVC 网站。
我过去曾在重型应用程序、多层应用程序中使用数据库来存储文件。
但现在我问自己,这对于网站来说是个好主意吗?从性能角度来看?
它对我来说有几个优点:
- 允许我轻松控制连接的用户是否有权显示图像(我的项目必需)
- 允许确保我们拥有一致的数据(否则我们可以拥有现有文件,但其中没有信息)数据库,相反,
- 我需要一个故障转移网络服务器,这些文件将从第三台服务器导入,所以如果这些文件在数据库中,我只需要有一个可用的 ASP.Net 网站和一个复制的数据库故障转移服务器,无需同步 但它
也有一些缺点:
- 有一些大文件(这是少数,但它会发生),比如 100-200MB,我不确定在数据库中存放这种文件是否好?(更多)就像一个问题;))
- 我不确定它会有好的表现吗?
你怎么认为?这合理吗?我在网上搜索过,但没有找到网站的一些说法。我的问题主要是关于 FILESTREAM VS FILESYSTEM,我确信 FileStream 慢一些,但是慢很多?因为如果只是百分之几,功能的获得是值得的。
I'm creating an Asp.Net MVC website.
I've in the past, for heavy application, multiple layer application, used the database to store files.
But now I'm questioning myself, is this a good idea for a website? In a performance view?
It has several pros to me:
- Allows me to control easily if the connected user has the right to display the image(Required for my project)
- Permits to be sure that we have consistent data(otherwise we can have an existing file but no info in the database and the opposite
- I need a fail-over webserver, and those files will be imported from a third server, so if those files are in the database, I only need to have a working ASP.Net website and a replicated database on the failover server, no need to sync files.
But it has some cons too:
- There SOME big files(it's a minority, but it will happen), like 100-200MB, and I'm not sure it's good to have this kind of file in a database?(it's more like a question ;) )
- I'm not sure that it will have good performances?
What do you think? Is this reasonable? I searched on the Internet, but I didn't found some kinds of arguments for website. My question is mostly about FILESTREAM VS FILESYSTEM, I'm sure that FileStream is slower, but a lot? Because if it's only some percent, the gain of functionality worth it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Microsoft Research 有一篇非常好的论文,名为 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.
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 efficient
if 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) or VARBINARY(MAX) columns, you can specify this file group for the large data:
Check out the MSDN intro on filegroups, and play around with it!
如果文件是系统的组成部分并且正在主动更改,并且它们必须与其他数据一起备份 - 您可以将它们存储在数据库中,但如果您使用,请尝试使用 FILESTREAM 字段sql server 2005+ 并且您的文件足够大 - 比如说 500k+
如果文件是静态内容,您可以仅使用数据库中的指针将它们存储在外部。这并不妨碍考虑您的所有自定义权限计算机。
在数据库中存储和使用文件通常比在文件系统中慢,但这一切都取决于您的需求。
If the files are integral and actively changing part of the system and them have to be backed up along with the other data - you can store them inside the DB, but try to use the FILESTREAM fields if you use sql server 2005+ and your files are big enough - say 500k+
If the files are static content, you can store them outside with only pointers in DB. This not prevents to take into account all your custom permissions machine.
Storing and working with files inside DB is usually slower, than in filesystem, but all depends on your needs.
为什么使用db而不是txt文件?因为它使用索引更快。将整个文件存储在数据库中从来都不是一个好的做法。使用 db 作为普通 img 文件的索引(指针)。
就您的优点/缺点而言:
如果您使用asp / php显示图像并将根图像文件夹设置在Web根目录之外,您可以轻松控制用户是否有权查看图像
您
将整个文件存储在数据库中会慢10倍(我知道mysql测试但它类似)对于 mssql - http:// blog.sitek.com.au/2008/03/comparison- Between-storing-imagesfiles-in-mysql-and-on-filesystem/)
如果您如果数据库中有文件,您将无法使用 CDN (http://en.wikipedia.org/wiki/Content_delivery_network)
Why use db instead of txt files? Because its faster it uses indexes. Storing whole files in db is never a good practice. Use db as index (pointers) to the normal img files.
As far as your pros / cons go:
you can easily controll if the user has the right to see the image if you display the image with asp / php and set root image folder outside web root
storing whole files in db is cca 10 times slower (I know for mysql testing but its similar for mssql - http://blog.sitek.com.au/2008/03/comparison-between-storing-imagesfiles-in-mysql-and-on-filesystem/)
if you have files in db you wont be able to use CDNs (http://en.wikipedia.org/wiki/Content_delivery_network)